[Java] 纯文本查看 复制代码
import java.util.Scanner;
public class NumTrans {
private static String input;
private static String[] units = {"","十","百","千","万","十","百","千","亿","十","百","千"};
private static String[] nums= {"零","一","二","三","四","五","六","七","八","九","十"};
private static String[] result;
public static void main(String[] args) {
input();
System.out.println(get(input));
}
public static void input() {
System.out.println("请输入一串数字:如123456,最多12位"); //扩展至12位
Scanner sc = new Scanner(System.in);
input = sc.nextLine();
}
public static String get(String input) {
StringBuilder out = new StringBuilder(); //StringBuilder接收
result = new String[input.length()];
for(int i=0;i<result.length;i++) {
result = String.valueOf(input.charAt(i));
}
int back = 0;
for(int i=0;i<result.length;i++) {
back = result.length-i-1; //判断位数的变量应拿出if循环
if(!result.equals("0")) {
out.append(nums[Integer.parseInt(result)]) ;
out.append(units[back]);
}else {
if(back==8){ //补整亿位
out.append(units[back]);
}
if(back==4&&(!out.toString().endsWith("亿"))){ //补整万位,且整亿不补
out.append(units[back]);
}
if(i==result.length-1) {
break;
}else {
if(!result[i+1].equals("0")) {
out.append(nums[0]) ;
}
}
}
}
return out.toString();
}
}