题解 | 学英语

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int a = in.nextInt();
        int million =a/1000000;
        int thousand =(a/1000)%1000;
        int hundred = a%1000;
        printMillion(million);
        printSthousand(thousand,million);
        printHundred(hundred);

    }
    private static void printMillion(int num) {
        if(num!=0){
             printNum(num);
             System.out.print("million ");
        }

    }
    private static void printSthousand(int num,int million) {
        if(num!=0){
            printHundred(num);
            System.out.print("thousand ");
        }else{
            if(million!=0)System.out.print("and ");
        }
    }
    private static void printHundred(int num) {
        int a = num / 100;
        int b = num % 100;
        if (a != 0) {
            printNum(a);
            System.out.print("hundred ");
            if (b != 0) {
                System.out.print("and ");
                printTy(b);
            }
        } else {
            if (b != 0)printTy(b);
        }
    }
    private static void printTy(int num) {
        int a = num / 10;
        int b = num % 10;
        switch (a) {
            case 0:
                printNum(b);
                break;
            case 1:
                switch (num) {
                    case 10:
                        System.out.print("ten ");
                        break;
                    case 11:
                        System.out.print("eleven ");
                        break;
                    case 12:
                        System.out.print("twelve ");
                        break;
                    case 13:
                        System.out.print("thirteen ");
                        break;
                    case 14:
                        System.out.print("fourteen ");
                        break;
                    case 15:
                        System.out.print("fifteen ");
                        break;
                    case 16:
                        System.out.print("sixteen ");
                        break;
                    case 17:
                        System.out.print("seventeen ");
                        break;
                    case 18:
                        System.out.print("eighteen ");
                        break;
                    case 19:
                        System.out.print("nineteen ");
                        break;
                }
                break;
            case 2:
                System.out.print("twenty ");
                printNum(b);
                break;
            case 3:
                System.out.print("thirty ");
                printNum(b);
                break;
            case 4:
                System.out.print("forty ");
                printNum(b);
                break;
            case 5:
                System.out.print("fifty ");
                printNum(b);
                break;
            case 6:
                System.out.print("sixty ");
                printNum(b);
                break;
            case 7:
                System.out.print("seventy ");
                printNum(b);
                break;
            case 8:
                System.out.print("eighty ");
                printNum(b);
                break;
            case 9:
                System.out.print("ninety ");
                printNum(b);
                break;
        }
    }
    private static void printNum(int num) {
        switch (num) {
            case 1:
                System.out.print("one ");
                break;
            case 2:
                System.out.print("two ");
                break;
            case 3:
                System.out.print("three ");
                break;
            case 4:
                System.out.print("four ");
                break;
            case 5:
                System.out.print("five ");
                break;
            case 6:
                System.out.print("six ");
                break;
            case 7:
                System.out.print("seven ");
                break;
            case 8:
                System.out.print("eight ");
                break;
            case 9:
                System.out.print("nine ");
                break;
        }
    }
}

全部评论

相关推荐

评论
1
收藏
分享
牛客网
牛客企业服务