用Java语言写出输入为123输出为壹贰叁
时间: 2024-06-10 17:07:01 浏览: 55
从键盘输入一个三位整数,逆序输出。如输入123,输出321。
好的,这个问题可以采用Java语言的字符串和数组处理。以下是示例代码:
```
public class JavaNumberToChinese {
private static final String[] CHINESE_NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
private static final String[] CHINESE_UNIT = {"", "拾", "佰", "仟"};
public static String numberToChinese(String input) {
if (input == null || input.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
int num = input.charAt(i) - '0';
if (num < 0 || num > 9) {
return "";
}
if (i != 0) {
sb.append(CHINESE_UNIT[input.length() - i - 1]);
}
sb.append(CHINESE_NUMBER[num]);
}
return sb.toString();
}
public static void main(String[] args) {
String input = "123";
String output = numberToChinese(input);
System.out.println(output); // 壹贰叁
}
}
```
阅读全文