输入一个整数,输出其对应汉语读法(如10020读作一万零二十、12000读作一万二千、300读作三百、1998读作一千九百九十九)。

  • Post author:
  • Post category:其他


import java.util.Scanner;
public class Lianxi {
    public static void main(String[] ags) {
        String[] shu = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        String[] zi = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万亿"};
        Scanner scan = new Scanner(System.in);
        while (true) {
            long a = scan.nextLong();
            if (a == 0) {
                System.out.println("零");
            }
            String result = String.valueOf(a);
            char[] b = result.toCharArray();
            String answer = "";
            int i, j,k=0;
            int length = b.length;
            for (i = 0; i < length; i++) {
                j = (int) b[i] - 48;
                if (j != 0) {
                    answer += shu[j] + zi[length - i - 1];//length-i-1:length-1因为数组第一个元素为0
                }
                else {
                    if (i == length - 1) {}
                    else {
                        if((length-i-1>3)&&(length-i-1<8)){
                            k++;
                        }
                        if (b[i + 1] != '0' && ((length - i - 1) == 4 || (length - i - 1) == 8)&&k!=4) {
                            answer += zi[length - i - 1];

                        }
                        else if (b[i + 1] != '0') {
                            answer += shu[j];
                        }
                        else {
                            if (b[i + 1] == '0' && ((length - i - 1) == 4 || (length - i - 1) == 8)&&k!=4) {
                                answer += zi[length - i - 1];}
                        }
                    }
                }
            }
            System.out.println(answer);
        }
    }
}



版权声明:本文为qq_62543372原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。