Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

java - How to convert 100 into ONE ZERO ZERO not ONE?

I have to call a method which is converting number to words and two others methods which are reversing and getting count of entered number and have to call both in first method but in case of number 100 it is giving output as ONE but it should give ONE ZERO ZERO.So my confusion is how to call count method in converting number method and to make give output as ONE ZERO ZERO with simple calling because I am using intelliJ and a beginner So I have to find out how to call get count method in first method which is number to word.

public static void numberToWords(int number) {
    if (number == 0) {
        System.out.println("Zero");
    } else if (number < 0) {
        System.out.println("Invalid Value");
    }

    int revInteger = reverse(number);
    while (revInteger != 0) {
        int modulus = revInteger % 10;
        revInteger /= 10;

        switch (modulus) {

            case 1:
                System.out.println("One");
                break;

            case 2:
                System.out.println("Two");
                break;

            case 3:
                System.out.println("Three");
                break;

            case 4:
                System.out.println("Four");
                break;

            case 5:
                System.out.println("Five");
                break;

            case 6:
                System.out.println("Six");
                break;

            case 7:
                System.out.println("Seven");
                break;

            case 8:
                System.out.println("Eight");
                break;

            case 9:
                System.out.println("Nine");
                break;

            default:
                break;
        }
    }
}


public static int reverse(int reversedNumber) {
    int revNumber = reversedNumber;
    int digit = 0;
    while (revNumber != 0) {
        int n = revNumber % 10;
        digit = digit * 10 + n;
        revNumber /= 10;
    }
    return digit;
}

public static int getDigitCount(int number) {
    int count = 0;
    if (number > 0) {
        while (number != 0) {
            number /= 10;
            ++count;
        }
        return count;
    } else if (number == 0) {
        return 1;
    }
    return -1;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead case blocks you can use a pre built map to replace your digits with word as below

public class NumToWord {

    public static void main(String[] args) {
        numberToWords(100);
    }

    public static final String[] numToWord = new String[10];
    static {
        numToWord[0]="Zero";
        numToWord[1]="One";
        numToWord[2]="Two";
        numToWord[3]="Three";
        numToWord[4]="Four";
        numToWord[5]="Five";
        numToWord[6]="Six";
        numToWord[7]="Seven";
        numToWord[8]="Eight";
        numToWord[9]="Nine";
    }
    public static void numberToWords(int number) {
        String numberStr = number + "";
        for (char a: numberStr.toCharArray()) {
            String word = numToWord[Integer.parseInt(a+"")];
            if(null!=word) {
                System.out.println(word);
            }
        }
    }
}



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...