重点在于0的处理,还有繁杂的字符、字符串、整数之间的转化,那些方法要记牢。
package algorithm;
import java.util.*;
public class Solution {
static char[] numArr = {'零','一','二','三','四','五','六','七','八','九'};
static String[] unitArr = {"","十","百","千","万"};
public static String numToChinese(int num) {
char[] newNum = String.valueOf(num).toCharArray();
int len = newNum.length;
StringBuilder res = new StringBuilder();
for(int i=0;i
int temp = Integer.parseInt(String.valueOf(newNum[i]));
boolean zeroFlag = temp==0 ? true : false;
if(zeroFlag) {
while(i
i++;
if(i!=len-1) {
res.append(numArr[0]);
}
}else {
res.append(numArr[temp]);
res.append(unitArr[len-i-1]);
}
}
return res.toString();
}
public static void main(String[] args) {
System.out.print(numToChinese(12345));
System.out.print("\n");
System.out.print(numToChinese(10000));
System.out.print("\n");
System.out.print(numToChinese(10001));
System.out.print("\n");
System.out.print(numToChinese(10100));
System.out.print("\n");
System.out.print(numToChinese(10101));
System.out.print("\n");
System.out.print(numToChinese(20890));
}
}
最新免费java,架构,大数据AI编程资料获取添加
薇信:18410263200
通过验证填写“111”(备注必填)
网友评论