【发布时间】:2017-12-24 03:27:29
【问题描述】:
//我们需要将xyzoneyctwothreefourvone转换为数字:1,234,1
public static void cal(String s){
HashMap<String, String> map= new HashMap<String, String>();
map.put("zero", "0");
map.put("one", "1");
map.put("two", "2");
map.put("three", "3");
map.put("four", "4");
s = "xyzoneyctwothreefourvone";
int temp;
StringBuilder strB = new StringBuilder();
ArrayList<StringBuilder> list = new ArrayList<StringBuilder>();
StringBuilder strB1 = new StringBuilder();
boolean isNum = false;
for (int i = 0; i <= s.length() - 1; i++) {
strB.append(s.charAt(i));
if (strB.length() >= 3 && map.containsKey(strB.toString())) {
isNum = true;
strB1.append(map.get(strB.toString().toLowerCase()));
strB = new StringBuilder();
} else if (strB.length() > 5) {
isNum = false;
strB1.append("+");
strB = new StringBuilder();
i = i - 5;
}
}
System.out.println(strB1.toString());
}
预期输出:+++1++234+1; 电流输出:+++1++234
【问题讨论】:
-
您的代码无法编译,缺少返回
标签: java string algorithm hashmap