题目: 奖金额转换成大写的七位数
分析:获取每一位数字,将数字转为大写的,数字前面添零,补成七位数,最后依次拼接单位
package text;
import java.util.Scanner;
public class MoneySwitch {public static void main(String[] args) {System.out.println("请输入金额");Scanner sc = new Scanner(System.in);//1,判断输入的金额是否有效int money = 0;while (true) {money = sc.nextInt();if (money >= 0 && money <= 9999999) {break;} else {System.out.println("输入错误,重新输入");}}String strMoney = "";//2,获取money里的每个数字while (money != 0) {int ge = money % 10;money = money / 10;//3,阿拉伯数字转换成大写//String c = switchNumber(ge);char c = switchNumber(ge);strMoney = c + strMoney;}System.out.println(strMoney);//4,添加零,构成7位数int count = 7 - strMoney.length();for (int i = 0; i < count; i++) {strMoney = "零" + strMoney;}System.out.println(strMoney);//5,添加单位String[] union = {"佰", "拾", "万", "仟", "佰", "拾", "元"};String result="";for (int i = 0; i < strMoney.length(); i++) {String a=union[i];result=result+strMoney.charAt(i)+a;}//6,打印结果System.out.println(result);}//定义一个方法,将数字转换成大写public static char switchNumber(int number) {//String[] arr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};//字符数组创建字符串char[] arr = {'零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'};//数组return arr[number];}
}
结果