2017-04-02 45 views
1

我在BlueJ上做這個程序時很掙扎。這裏是我的代碼:在Java中的字數

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(); 

    } 
} 

}  
} 

所以我的問題是我這個執行時,它只有當我進入第一個數字我的屏幕上出現單詞「號碼」一次。例如: 編號:1 =>一個和 2 =>兩個 現在我要將「Number:2」打印出來2.如果我的代碼有任何問題,是否有人可以看看。謝謝!

+0

只是有一個簡單的哈希映射來完成從單詞到您遇到的實際數字的映射。 – Mox

+0

對不起,請你解釋一下。我不明白。每次輸入新號碼時,我都想打印出「Number」一詞。謝謝! – COI

+0

您的system.out.println(「Number」)只執行一次。如果你想讓它出現在每個數字的前面,把它放在循環中。對不起以前的困惑。似乎我誤解了你的問題 – Mox

回答

-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(); 

     } 
    } 

    }  
    } 
+0

如果有人爲他們做功課,人們應該如何學習編程?這個任務是爲了教導學生而做的,如果你爲他們工作,他們就不會學到任何東西,除了如何複製/粘貼來自其他人的代碼,這會導致他們獲得他們沒有獲得的學位,以及他們以後的工作沒有資格去做。 –

0

添加「號碼:」在每一個打印語句,我認爲這是你想要的。

System.out.println("Number:" + result);