1002 Write this number (Grade B)

Title Description:
Read in a positive integer n, calculate the sum of its digits, and write out the sum of each digit in Chinese Pinyin.
Input format:

Each test input contains one test case, i.e. the value of natural number n is given. Here n is guaranteed to be less than 10 100.
Output format:

For each of the digits that output n in a line, there is a space between the Pinyin digits, but there is no space after the last Pinyin digit in a line.
Input sample:

1234567890987654321123456789

Output sample:

yi san wu

Solution:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        int sum = 0;
        char[] ch = n.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            sum += (int) (ch[i] - '0');
        }
        StringBuilder sb = new StringBuilder();
        while (sum > 0) {
            int a = sum % 10;
            switch (a) {
                case 0:
                    sb.append("ling ");
                    break;
                case 1:
                    sb.append("yi ");
                    break;
                case 2:
                    sb.append("er ");
                    break;
                case 3:
                    sb.append("san ");
                    break;
                case 4:
                    sb.append("si ");
                    break;
                case 5:
                    sb.append("wu ");
                    break;
                case 6:
                    sb.append("liu ");
                    break;
                case 7:
                    sb.append("qi ");
                    break;
                case 8:
                    sb.append("ba ");
                    break;
                case 9:
                    sb.append("jiu ");
                    break;
            }
            sum /= 10;
        }
        String s = sb.toString();
        String[] sp = s.split(" ");
        for (int i = sp.length - 1; i >= 0; i--) {
            if (i == 0) System.out.print(sp[i]);
            else System.out.print(sp[i] + " ");
        }
    }

}

Tags: less Java

Posted on Sun, 06 Oct 2019 09:49:54 -0700 by nicolam1