//题目,做一个输入金额数字,输出转换成中文的金额名称。
public class Test {
public static void main(String\[\] args) {
System.out.println("欢迎使用数字转换器(Designed by wujj)!!");
String in;
boolean isRunning = true;
Scanner scanner = new Scanner(System.in);
while (isRunning) {
System.out.println("请输入一串纯整数:");
in = scanner.next();
String input = in.trim();
if (!isNum(input)) {
System.out.println("输入类型错误!!!你应该输入12位以内纯正整数!!!");
continue;
} else {
System.out.println("转换成中文数字为:" + num2Ch(input));
}
System.out.println("是否继续查询? 1.是 2.不,谢谢");
int isContinue = scanner.nextInt();
if (isContinue == 1) {
continue;
} else if (isContinue == 2) {
System.out.println("程序结束!欢迎下次使用!!!");
isRunning = !isRunning;
} else {
System.out.println("指令输入错误,直接退出!!");
System.out.println("程序结束!欢迎下次使用!!!");
isRunning = !isRunning;
}
}
System.out.println("请输入任意数字退出控制台...");
in = scanner.next();
scanner.close();
}
private static String num2Ch(String input) {
input = Long.parseLong(input) + "";
if (input.equals("0")) {
return "零";
}
// int length = input.length();
// char\[\] arr1 = { 'O', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '百',
// '千', '万', '亿' };
// char\[\] arr2 = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖', '拾', '佰',
// '仟', '万', '亿' };
StringBuffer s = new StringBuffer();
switch (4 - (input.length() % 4)) {
case 1:
s = new StringBuffer("0").append(input);
break;
case 2:
s = new StringBuffer("00").append(input);
break;
case 3:
s = new StringBuffer("000").append(input);
break;
default:
s = new StringBuffer("").append(input);
}
String\[\] arr = new String\[s.length() / 4\];
for (int i = 0; i < s.length(); i = i + 4) {
if (i + 4 == s.length())
arr\[i / 4\] = s.toString().substring(i);
else
arr\[i / 4\] = s.toString().substring(i, i + 4);
}
StringBuffer output = new StringBuffer("");
switch (s.length() / 4) {
case 3:
output = output.append(trans(arr\[s.length() / 4 - 3\])).append("亿");
case 2:
if (Integer.parseInt(arr\[s.length() / 4 - 2\]) == 0 && Integer.parseInt(arr\[s.length() / 4 - 3\]) != 0)
output = output.append("零");
else
output = output.append(trans(arr\[s.length() / 4 - 2\])).append("万");
case 1:
output = output.append(trans(arr\[s.length() / 4 - 1\]));
break;
}
return output.toString();
}
// 四位数转换函数
private static String trans(String string) {
String s = Integer.parseInt(string) + "";
StringBuffer output = new StringBuffer("");
String\[\] arr = { "", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
// for (int i = 0; i < s.length(); i++) {
// if (s.charAt(i) != 0) {
// s = s.substring(i);
// break;
// }
// }
if ((Integer.parseInt(s) + "").equals("")) {
return "";
} else {
switch (s.length()) {
case 4:
if (Integer.parseInt(string.charAt(0) + "") == 0) {
output = output.append("零");
}else {
output = output.append(arr\[Integer.parseInt(s.charAt(0) + "")\]).append("千");
}
case 3:
if (Integer.parseInt(string.charAt(3) + "") != 0 && Integer.parseInt(string.charAt(1) + "") == 0 && Integer.parseInt(string.charAt(0) + "") != 0) {
output = output.append("零");
}else {
output = output.append(arr\[Integer.parseInt(string.charAt(1) + "")\]);
}
if (Integer.parseInt(string.charAt(1) + "") != 0) {
output = output.append("百");
}
case 2:
if (Integer.parseInt(string.charAt(3) + "") != 0 && Integer.parseInt(string.charAt(2) + "") == 0 && Integer.parseInt(string.charAt(1) + "") != 0
&& Integer.parseInt(string.charAt(0) + "") != 0) {
output = output.append("零");
}else{
output = output.append(arr\[Integer.parseInt(string.charAt(2) + "")\]);
}
if (Integer.parseInt(string.charAt(2) + "") != 0) {
output = output.append("十");
}
case 1:
if (Integer.parseInt(string.charAt(3) + "") != 0) {
output = output.append(arr\[Integer.parseInt(string.charAt(3) + "")\]);
}
break;
}
return output.toString();
}
}
// 判断是否是纯数字输入
private static boolean isNum(String s) {
String num = "0123456789";
int length = 0;
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < num.length(); j++) {
if (s.charAt(i) == num.charAt(j)) {
length++;
break;
}
}
}
return (s.trim().length() != 0 && length == s.length() && length <= 12 && length >= 0) ? true : false;
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章