输入一串数字,输出大写金额
import java.util.Scanner;
public class test {
/**
* 获取数位
*
* @param i
* @return
*/
private static String getDigitPlace(int i) {
switch (i) {
case 2:
case 6:
case 10:
return "拾";
case 3:
case 7:
case 11:
return "佰";
case 4:
case 8:
case 12:
return "仟";
case 5:
case 13:
return "万";
case 9:
return "亿";
default:
return "";
}
}
/**
* 输出数字的大写
*
* @param a
* @return
*/
private static String getUpperCase(int a) {
switch (a) {
case 49:
return "壹";
case 50:
return "贰";
case 51:
return "叁";
case 52:
return "肆";
case 53:
return "伍";
case 54:
return "陆";
case 55:
return "柒";
case 56:
return "捌";
case 57:
return "玖";
default:
return "";
}
}
/**
* 输入一串数字,输出大写金额,如123 输出壹佰贰拾叁整。
*
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入一串数字");
String num = input.nextLine();
// for循环中尽量不要用String +=的方式拼接,可以用StringBuilder
boolean perviousIsZero = false;
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < num.length(); i++) {
int a = num.charAt(i);
// 判断当前是否为零
if (a == 48) {
perviousIsZero = true;
continue;
}
// 判断数字前一个是否是零且当前数字不为零
if (perviousIsZero) {
if (a != 48) {
stringBuilder.append("零");
perviousIsZero = false;
} else {
// 都是零就跳过
continue;
}
}
// 获取数字大写
stringBuilder.append(getUpperCase(a));
// 获取数位
stringBuilder.append(getDigitPlace(num.length() - i));
}
System.out.println(stringBuilder.toString() + "整");
}
}