Java中单词的数字

时间:2017-04-02 02:59:47

标签: java bluej

我在BlueJ上执行此程序时遇到了困难。这是我的代码:

public class NumberToWords {
    public static void main(String[] args) {
        System.out.print("Number: ");
        int value = In.nextInt();
        int onesDigit;
        int tensDigit;
        String [] ones = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
            "eleven", "tweleve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

        String [] tens = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

        String [] hundreds = {"one hundred", "two hundred", "three hundred", "four hundred", "five hundred", 
            "six hundred", "seven hundred", "eight hundred", "nine hundred" }; 
  while(value != -1)
 {
   if (value < 20)
    {

        String result = ones[value];
        System.out.println("" + result);
        value = In.nextInt();
    }
   if (value > 20 && value < 100)
    {
        tensDigit = value/10;
        onesDigit = value%10;
        System.out.print("" + ones[onesDigit] + " " + tens[tensDigit]);
        value = In.nextInt();

    }
 }

}    
}

所以我的问题是,当我执行此操作时,当我输入第一个数字时,我的屏幕上只显示“Number”字样。例如: 数量:1 =&gt;一个和 2 =&gt;二 现在我希望“数字:2”将打印出2.如果我的代码出现任何问题,有人可以查看。谢谢!

1 个答案:

答案 0 :(得分:-1)

以下代码可帮助您获得所需的输出:

    public class NumberToWords  {
        public static void main(String[] args) {



            int onesDigit;
            int tensDigit;
            String [] ones = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
                "eleven", "tweleve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

            String [] tens = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

            String [] hundreds = {"one hundred", "two hundred", "three hundred", "four hundred", "five hundred", 
                "six hundred", "seven hundred", "eight hundred", "nine hundred" };
            System.out.print("Number: ");
          int value = In.nextInt();
      while(value != -1)
     {

       if (value < 20)
        {

            String result = ones[value];
            System.out.println("" + result);
            System.out.print("Number: ");
            value = In.nextInt();
        }
       if (value > 20 && value < 100)
        {
            tensDigit = value/10;
            onesDigit = value%10;
            System.out.print("" + ones[onesDigit] + " " + tens[tensDigit]);
            System.out.print("Number: ");
            value = In.nextInt();

        }
     }

    }    
    }