本帖最后由 Jet'aime 于 2017-2-10 21:18 编辑
[Java] 纯文本查看 复制代码 public static String changeMoney() {
//定义数组存储零到玖,跟下标对应
String[] numArr = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
//初始化键盘录入值
int money = -1;
//录入整数
while (true) {
System.out.println("请输入一个0-999之间的整数:");
Scanner sc = new Scanner(System.in);
try {
money = sc.nextInt();
} catch (Exception e) {
System.out.println("请输入数字!");
continue;
}
if (money >= 0 && money <= 999) {
break;
}
}
///分别获取录入数字的个位十位和百位
int ge = money % 10;
int shi = money / 10 % 10;
int bai = money / 100;
//找到每位数字对应的汉字并拼接
String str = "";
if (bai != 0) {
str = numArr[bai] + "佰" + numArr[shi] + "拾" + numArr[ge];
} else if (shi != 0) {
str = numArr[shi] + "拾" + numArr[ge];
} else if (ge != 0) {
str = numArr[ge];
} else {
str = "零";
}
return str;
} |