金额最多七位
例:2135----->零佰零拾零万贰仟壹佰叁拾伍元
import java.util.Scanner; public class AmountConversion2 { public static void main(String[] args) { //输入金额 Scanner sc = new Scanner(System.in); System.out.println("请输入不超过7位数字:"); //循环直到键盘输入的是不超过7位数字 while (true){ int money = sc.nextInt(); if (money>=0&&money<=9999999) { System.out.println("转换结果为:"); System.out.println(split2(split1(split(money)))); break; }else { System.out.println("输入的数字超过7位,请重新输入:"); } } } //小写数字大写(2135转化为贰壹叁伍) public static String split(int money){ String[] arr ={"零","壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌","玖"}; String result = ""; while (money != 0){ int ge = money % 10; result = arr[ge] + result; money= money / 10; } return result; } //贰壹叁伍转化为零零零贰壹叁伍 public static String split1(String sh){ String result =sh; int count = 7-result.length(); for (int i = 0; i < count; i++) { result = "零" + result; } return result; } //零零零贰壹叁伍转化为零佰零拾零万贰仟壹佰叁拾伍元 public static String split2(String sh){ String[] arr ={"佰","拾","万","仟","佰","拾","元"}; String result = ""; for (int i = 0; i < sh.length(); i++) { result = result + sh.charAt(i) + arr[i]; } return result; } }
运行结果
请输入不超过7位数字:
11111111
输入的数字超过7位,请重新输入:
2135
转换结果为:
零佰零拾零万贰仟壹佰叁拾伍元进程已结束,退出代码0