用Java写一个程序:在键盘输入阿拉伯数字,将它转化为中文。如:2020转化为二零二零
时间: 2024-06-10 22:06:55 浏览: 53
好的,这个问题很简单。以下是 Java 代码的示例:
```
import java.util.Scanner;
public class NumberToChinese {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个阿拉伯数字:");
String number = scanner.nextLine();
String[] digits = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
String[] units = { "", "十", "百", "千" };
StringBuilder sb = new StringBuilder();
for (int i = 0; i < number.length(); i++) {
int digit = number.charAt(i) - '0';
int unitIndex = number.length() - i - 1;
if (digit != 0) {
sb.append(digits[digit]);
sb.append(units[unitIndex % 4]);
}
if (unitIndex % 4 == 0) {
sb.append("万");
}
}
if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '零') {
sb.deleteCharAt(sb.length() - 1);
}
System.out.println("中文数字是:" + sb.toString());
}
}
```
以上程序会提示用户输入一个阿拉伯数字,然后将其转化为中文,并输出。例如输入"2020",则输出"中文数字是:二零二零"。
阅读全文