没想到答案可以这么简洁,自己做的时候完全没有想到。
package heinma.java.String;
import java.util.Scanner;
public class text1 {
public static void main(String[] args) {
//键盘录入金额
Scanner sc = new Scanner(System.in);
int money;
while (true) {
System.out.println("请输入金额");
money = sc.nextInt();
//判断是否在范围内
if(money>=0&&money<=9999999){
break;
}
else {
System.out.println("金额无效请重新输入");
}
}
//定义一个变量来表示金额的大写
String ChineseNumberlong ="";
//定义一个数组为金额的大写
String arr[]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
//定义一个累加器记录有多少位
int count=0;
//得到money的每一个数字并中文大写
while (money!=0){
//从右往左获取数字,所以第一个是个位的
int number=money%10;
String ChineseNumber =arr[number];
//转换之后反着拼接(确实没想到)
//第一次:""+"陆"="陆"
//第二次:"伍"+"陆"="伍陆"
ChineseNumberlong=ChineseNumber+ChineseNumberlong;
money=money/10;
count++;
}
//判断得到的数字和七位差几位
count = 7-ChineseNumberlong.length();
for (int i = 0; i < count; i++) {
//一样反着加上差的位数的零
ChineseNumberlong = "零"+ ChineseNumberlong;
}
//验证是否有误
System.out.println(ChineseNumberlong);
//定义的插入的单位
//数组位数一样,直接顺序输出就行!!!!!!!!
String arr2[] = {"佰","拾","万","仟","佰","拾","元"};
for (int i = 0; i < ChineseNumberlong.length(); i++) {
char car = ChineseNumberlong.charAt(i);
System.out.print(car);
System.out.print(arr2[i]);
}
}
}
结果图