Java|PTA乙级|1002 写出这个数 (20分)|解析

解析答案如下:

import java.util.Scanner;
/*定义字符串数组,再将每个强制转换为(Tnteger.parseInt)
整型数组,再相加,并再次转为字符串数组(split())
*/

public class Main {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String shuzi=sc.next();
        String[] s=shuzi.split("");
        //记录数字
        int a=0;
        for(int t=0;t<s.length;t++){
            a+=Integer.parseInt(s[t]);
        }
        //System.out.println(a);
        String b=String.valueOf(a);
        String[] h=b.split("");
        for(int i=0;i<h.length;i++){
            switch (Integer.parseInt(h[i])){
                case 1:
                    if(i==(h.length-1)){
                        System.out.print("yi");
                    }else{
                        System.out.print("yi"+" ");
                    }
                    break;
                case 0:
                    if(i==(h.length-1)){
                        System.out.print("ling");
                    }else{
                        System.out.print("ling"+" ");
                    }
                    break;
                case 2:
                    if(i==(h.length-1)){
                        System.out.print("er");
                    }else{
                        System.out.print("er"+" ");
                    }
                    break;
                case 3:
                    if(i==(h.length-1)){
                        System.out.print("san");
                    }else{
                        System.out.print("san"+" ");
                    }
                    break;
                case 4:
                    if(i==(h.length-1)){
                        System.out.print("si");
                    }else{
                        System.out.print("si"+" ");
                    }
                    break;
                case 5:
                    if(i==(h.length-1)){
                        System.out.print("wu");
                    }else{
                        System.out.print("wu"+" ");
                    }
                    break;
                case 6:
                    if(i==(h.length-1)){
                        System.out.print("liu");
                    }else{
                        System.out.print("liu"+" ");
                    }
                    break;
                case 7:
                    if(i==(h.length-1)){
                        System.out.print("qi");
                    }else{
                        System.out.print("qi"+" ");
                    }
                    break;
                case 8:
                    if(i==(h.length-1)){
                        System.out.print("ba");
                    }else{
                        System.out.print("ba"+" ");
                    }
                    break;
                case 9:
                    if(i==(h.length-1)){
                        System.out.print("jiu");
                    }else{
                        System.out.print("jiu"+" ");
                    }
                    break;
            }
        }
    }
}
posted @ 2021-01-30 14:14  牛家俊  阅读(108)  评论(0编辑  收藏  举报