将2134转换为零佰零拾零万贰仟壹佰叁拾肆元。
package example;
import java.util.Scanner;
public class demo02 {
public static void main(String[] args) {
//1.键盘输入金额
Scanner sc = new Scanner(System.in);
int money = 0;
while (true) {
System.out.println("请输入一个金额:");
money = sc.nextInt();
if (money >= 0 && money <= 9999999) {
break;
} else {
System.out.println("金额不合法");
}
}
//2.得到money的每一位数字
String moneyStr_reverse = "";//表示钱的大写
while (money > 0) {
int num = money % 10;
String bigNumber = getCash(num);//将数字转换为大写
moneyStr_reverse += bigNumber;
money = money / 10;
}
//3.补零且颠倒
while (moneyStr_reverse.length() <7) {
moneyStr_reverse += "零";
}
String moneyStr = "";
for (int i = moneyStr_reverse.length() - 1; i >= 0; i--) {
moneyStr += moneyStr_reverse.charAt(i);
}
//4.插入单位
String[] moneyresult={"佰","拾","万","仟","佰","拾","元"};
for(int i=0;i<moneyStr.length();i++){
moneyresult[i] =moneyStr.charAt(i)+moneyresult[i];
}
for(int i=0;i<moneyresult.length;i++){
System.out.print(moneyresult[i]);
}
}
//将数字变成大写
public static String getCash(int num) {
String[] arr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
return arr[num];
}
}
请输入一个金额:
2134
零佰零拾零万贰仟壹佰叁拾肆元