求大神看段代码谢谢呀!
下面这段代码method方法参数不能带0,带0输出就乱了,求高手解决呀!0怎么处理呀!没分了
public class Convert {
public void method(String num){
String[] arrUnit = {"元","十","百","千","万"}; // 计量单位
String[] arrNum = {"零","一","二","三","四","五","六","七","八","九"}; //大写数字
String[] strNum = num.split("|");
int nNumCount = strNum.length - 2;
// String newStr = "";
for(int i=0; i<strNum.length; i++){
if(strNum[i] != null && !"".equals(strNum[i])){
int n = Integer.parseInt(strNum[i]);
switch(n){
case 0 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 1 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 2 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 3 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 4 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 5 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 6 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 7 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 8 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
case 9 : System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]); break;
}
}
}
}
public static void main(String args[])
{
new Convert().method("1111");
}
} 逻辑
[解决办法]
你这代码有其他问题吧。。。
case 0:
//System.out.print(arrNum[n] + "" + arrUnit[nNumCount--]);
nNumCount--;
System.out.print(arrNum[n]);
break;
[解决办法]
这段程序如果要考虑严格的话,需要判断输入的num是否为合法的数字类型。暂时先不管这个校验
把你的程序稍微修改了下,不过结果倒是可以满足
public void method(String num) {
String[] arrUnit = { "元", "十", "百", "千", "万" }; // 计量单位
String[] arrNum = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" }; // 大写数字
String[] strNum = num.split("
[解决办法]
");
int nNumCount = strNum.length - 2;
//System.out.println("nNumCount:" + nNumCount);
StringBuilder newStr = new StringBuilder();
for (int i = 0; i < strNum.length; i++) {
if (strNum[i] != null && !"".equals(strNum[i])) {
int n = Integer.parseInt(strNum[i]);
//System.out.println("nNumCount:" + nNumCount + "n:" + n);
switch (n) {
case 0:
if (!num.matches("[1-9]\\d*0$")
[解决办法]
i != strNum.length -1) {
newStr.append(arrNum[n]);
}
if (i == (strNum.length-1)) {
newStr.append(arrUnit[nNumCount]);
}
nNumCount--;
break;
case 1:
newStr.append(arrNum[n] + "" + arrUnit[nNumCount--]);
break;
case 2:
newStr.append(arrNum[n] + "" + arrUnit[nNumCount--]);
break;
case 3:
newStr.append(arrNum[n] + "" + arrUnit[nNumCount--]);
break;
case 4:
newStr.append(arrNum[n] + "" + arrUnit[nNumCount--]);
break;
case 5:
newStr.append(arrNum[n] + "" + arrUnit[nNumCount--]);
break;
case 6:
newStr.append(arrNum[n] + "" + arrUnit[nNumCount--]);
break;
case 7:
newStr.append(arrNum[n] + "" + arrUnit[nNumCount--]);
break;
case 8:
newStr.append(arrNum[n] + "" + arrUnit[nNumCount--]);
break;
case 9:
newStr.append(arrNum[n] + "" + arrUnit[nNumCount--]);
break;
}
}
}
String result = newStr.toString().replaceAll("零{2,3}", "零").replaceAll("零元", "元");
System.out.println(result);
}