繁体   English   中英

如何在程序中以文字形式输出一百和十几岁的数字

[英]How can I output the hundred and teens numbers in words in my program

我需要帮助以文字显示我的一百和十几岁的数字。 例如,如果我输入116。我的程序将输出一百零六,而不是一百一十六。 除青少年数字外,我输入的所有其他数字均有效。

我会在您的代码中更改4件事:

第一:

使用int而不是double进行输入

int numInput = Integer.parseInt(br.readLine());//user inputs number  

第二:

为了在int获得适当的数字位置:

int hundredsDigit = (numInput % 1000) / 100;
int tensDigit = (numInput % 100) / 10;
int onesDigit = numInput % 10;

代替:

double hundredsDigit=Math.floor((numInput%1000)/100);
double tensDigit = Math.floor((numInput % 100) / 10);
double onesDigit = numInput % 10;

第三:

110-119范围的else条件必须在100-999之前(技术上应为120-999)

第四:

您的teens方法将原始的numInput作为参数。

您需要采取的是onesDigit来确定它是哪个“青少年”

所以应该像这样调用:

teens(onesDigit);

必须在[10-19]和[110-119]条件下更改此呼叫

您的青少年的新方法应如下所示:

public static void teens(int onesDigit) {
    if (onesDigit == 0) {
        System.out.print("Ten ");
    }
    if (onesDigit == 1) {
        System.out.print("Eleven ");
    }
    if (onesDigit == 2) {
        System.out.print("Twelve ");
    }
    if (onesDigit == 3) {
        System.out.print("Thirteen ");
    }
    if (onesDigit == 4) {
        System.out.print("Fourteen ");
    }
    if (onesDigit == 5) {
        System.out.print("Fifteen ");
    }
    if (onesDigit == 6) {
        System.out.print("Sixteen ");
    }
    if (onesDigit == 7) {
        System.out.print("Seventeen ");
    }
    if (onesDigit == 8) {
        System.out.print("Eighteen ");
    }
    if (onesDigit == 9) {
        System.out.print("Nineteen ");
    }
}//closes teens method

发生这种情况是因为您在检查数字是否在[100,119]范围内之前先检查它是否在[100,999]范围内,请更改ifs的顺序,这样就可以正常工作。

万一您不想这样做, 我没有写这个

public static String intToText(int n) {
    if (n < 0)
        return "Minus " + intToText(-n);
    else if (n == 0)
        return "Zero";
    else if (n <= 19)
        return oneToNineteen[n - 1] + " ";
    else if (n <= 99)
        return twentyToNinety[n / 10 - 2] + " " + intToText(n % 10);
    else if (n <= 199)
        return "One Hundred " + intToText(n % 100);
    else if (n <= 999)
        return intToText(n / 100) + "Hundred " + intToText(n % 100);
    else if (n <= 1999)
        return "One Thousand " + intToText(n % 1000);
    else if (n <= 999999)
        return intToText(n / 1000) + "Thousand " + intToText(n % 1000);
    else if (n <= 1999999)
        return "One Million " + intToText(n % 1000000);
    else if (n <= 999999999)
        return intToText(n / 1000000) + "Million " + intToText(n % 1000000);
    else if (n <= 1999999999)
        return "One Billion " + intToText(n % 1000000000);
    else
        return intToText(n / 1000000000) + "Billion " + intToText(n % 1000000000);
}

比较时从最小的数字到最大的数字。

第一个条件:

if((numInput>=10)&&(numInput<=19)){

第二:

else if((numInput>=20)&&(numInput<=99)){

第三:

else if((numInput>100)&&(numInput<=119)){

第四:

else if((numInput>=100)&&(numInput<=999)){

你应该把

else if((numInput>100)&&(numInput<=119))

更具包容性的条款

else if((numInput>=100)&&(numInput<=999))

这里发生的是首先搜索更大的范围,并且tens函数不会为1输出任何内容。

你的其他

else if((numInput>100)&&(numInput<=119)){
        hundreds(hundredsDigit);
        System.out.print(" ");
        teens(numInput);
    }

必须放在更高一级...

else if((numInput>=100)&&(numInput<=999)){
        hundreds(hundredsDigit);
        System.out.print(" ");
        tens(tensDigit);
        System.out.print(" ");
        ones(onesDigit);

    }

也将满足条件,您输入的“ 116”将永远不会到达那里...

很好玩 它具有以下问题:

-仅处理整数。 没有多头,双打等
-不处理零的单一情况。
-由于数字=数字* -1而在Integer.MIN_VALUE处失败

否则,它似乎起作用。 这样做的动机是我无法忍受大量的“ if-else”代码。

public class NumbersToWords {
    private static final Map<Integer,String> NUM_TO_WORD = new HashMap<Integer, String>();
    private static final List<Integer> KEYS = new ArrayList<Integer>(30);

    static {
        NUM_TO_WORD.put(0,"zero");
        NUM_TO_WORD.put(1,"one");
        NUM_TO_WORD.put(2,"two");
        NUM_TO_WORD.put(3,"three");
        NUM_TO_WORD.put(4,"four");
        NUM_TO_WORD.put(5,"five");
        NUM_TO_WORD.put(6,"six");
        NUM_TO_WORD.put(7,"seven");
        NUM_TO_WORD.put(8,"eight");
        NUM_TO_WORD.put(9,"nine");
        NUM_TO_WORD.put(10,"ten");
        NUM_TO_WORD.put(11,"eleven");
        NUM_TO_WORD.put(12,"twelve");
        NUM_TO_WORD.put(13,"thirteen");
        NUM_TO_WORD.put(14,"fourteen");
        NUM_TO_WORD.put(15,"fifteen");
        NUM_TO_WORD.put(16,"sixteen");
        NUM_TO_WORD.put(17,"seventeen");
        NUM_TO_WORD.put(18,"eighteen");
        NUM_TO_WORD.put(19,"nineteen");
        NUM_TO_WORD.put(20,"twenty");
        NUM_TO_WORD.put(30,"thirty");
        NUM_TO_WORD.put(40,"forty");
        NUM_TO_WORD.put(50,"fifty");
        NUM_TO_WORD.put(60,"sixty");
        NUM_TO_WORD.put(70,"seventy");
        NUM_TO_WORD.put(80,"eighty");
        NUM_TO_WORD.put(90,"ninety");
        NUM_TO_WORD.put(100,"hundred");
        NUM_TO_WORD.put(1000,"thousand");
        NUM_TO_WORD.put(1000000,"million");
        NUM_TO_WORD.put(1000000000,"billion");

        KEYS.addAll(NUM_TO_WORD.keySet());

        Collections.sort(KEYS);
    }

    public static void main(String[] args){
        int[] testValues = {24,4,543755,12,10000,123000,123,Integer.MAX_VALUE, -456};
        NumbersToWords ntw = new NumbersToWords();
        for(int i : testValues){
            System.out.println(i + " -> " + ntw.getWords(i));
        }
    }

            /* called recursively */
    public String getWords(int number){
        boolean isNegative = number < 0;
        if(isNegative){
            number = number * -1;
        }

        if(number < 100){
            return getWordLessThanHundred(number);
        }

        StringBuilder words = new StringBuilder(50);
        int key = getKey(number);
        if(isNegative){
            words.append("negative ");
        }
        words.append(getWords(number/key)) 
             .append(" ").append(NUM_TO_WORD.get(key)) // get the largest placeholder word
             .append(" ").append(getWords(number % key)); // get the rest
        return words.toString();
    }

    private String getWordLessThanHundred(int number){
        if(number == 0){
            return "";
        }

        if(number < 21){
            return NUM_TO_WORD.get(number);
        }

        int key = getKey(number);
        return NUM_TO_WORD.get(key) + " " + NUM_TO_WORD.get(number - key);
    }

    private int getKey(int number){
       for(int i = 0; i<KEYS.size();i++){
           int value = KEYS.get(i);
           if(i > 0 && number < value){
               return KEYS.get(i - 1);
           }else if(number == value){
               return value;
           }
       }
       return KEYS.get(KEYS.size() - 1);
    }   
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM