2014-09-19 60 views
0

我有這個課程的作業,我正在回收,我遇到的問題是我過度思考解決方案。我必須創建一個將四位數字轉換爲單詞的程序。從Java到Word轉換的次數

(例如:1134變成「壹壹三四」)

我有一個基本的代碼,但它的笨重和醜陋。我也只允許使用基本的if和switch語句,我們也必須使用switch語句。

我在想這個嗎?我無法弄清楚如何縮短這段代碼,而且我只想使用一個switch語句而沒有while循環。這是甚至可能的還是這個儘可能短。

這是我的代碼。

import java.util.Scanner; 
 

 
public class NumberToWords { 
 

 
\t public static void main(String[] args) { 
 
\t \t //Set up scanner. 
 
\t \t Scanner kb = new Scanner(System.in); 
 
\t \t 
 
\t \t //Ask for a 4 digit integer. 
 
\t \t System.out.println("Enter a 4 digit number."); 
 
\t \t 
 
\t \t //Store 4 digit number into a variable 
 
\t \t int number = kb.nextInt(); 
 
\t \t 
 
\t \t //Seperate number into digits. 
 
\t \t int digit4 = number%10; 
 
\t \t number = number/10; 
 
\t \t int digit3 = number%10; 
 
\t \t number = number/10; 
 
\t \t int digit2 = number%10; 
 
\t \t number = number/10; 
 
\t \t int digit1 = number%10; 
 
\t \t number = number/10; 
 
\t \t 
 
\t \t //Set up a switch statement to read through the number. 
 
\t \t switch (digit1) 
 
\t \t { 
 
\t \t case 1: System.out.print("One ");break; 
 
\t \t case 2: System.out.print("Two "); break; 
 
\t \t case 3: System.out.print("Three "); break; 
 
\t \t case 4: System.out.print("Four "); break; 
 
\t \t case 5: System.out.print("Five "); break; 
 
\t \t case 6: System.out.print("Six "); break; 
 
\t \t case 7: System.out.print("Seven "); break; 
 
\t \t case 8: System.out.print("Eight "); break; 
 
\t \t case 9: System.out.print("Nine "); break; 
 
\t \t case 0: System.out.print("Zero "); break; 
 
\t \t default: System.out.print(""); break; 
 
\t \t } 
 
\t \t switch (digit2) 
 
\t \t { 
 
\t \t case 1: System.out.print("One ");break; 
 
\t \t case 2: System.out.print("Two "); break; 
 
\t \t case 3: System.out.print("Three "); break; 
 
\t \t case 4: System.out.print("Four "); break; 
 
\t \t case 5: System.out.print("Five "); break; 
 
\t \t case 6: System.out.print("Six "); break; 
 
\t \t case 7: System.out.print("Seven "); break; 
 
\t \t case 8: System.out.print("Eight "); break; 
 
\t \t case 9: System.out.print("Nine "); break; 
 
\t \t case 0: System.out.print("Zero "); break; 
 
\t \t default: System.out.print(""); break; 
 
\t \t } 
 
\t \t switch (digit3) 
 
\t \t { 
 
\t \t case 1: System.out.print("One ");break; 
 
\t \t case 2: System.out.print("Two "); break; 
 
\t \t case 3: System.out.print("Three "); break; 
 
\t \t case 4: System.out.print("Four "); break; 
 
\t \t case 5: System.out.print("Five "); break; 
 
\t \t case 6: System.out.print("Six "); break; 
 
\t \t case 7: System.out.print("Seven "); break; 
 
\t \t case 8: System.out.print("Eight "); break; 
 
\t \t case 9: System.out.print("Nine "); break; 
 
\t \t case 0: System.out.print("Zero "); break; 
 
\t \t default: System.out.print(""); break; 
 
\t \t } 
 
\t \t switch (digit4) 
 
\t \t { 
 
\t \t case 1: System.out.print("One ");break; 
 
\t \t case 2: System.out.print("Two "); break; 
 
\t \t case 3: System.out.print("Three "); break; 
 
\t \t case 4: System.out.print("Four "); break; 
 
\t \t case 5: System.out.print("Five "); break; 
 
\t \t case 6: System.out.print("Six "); break; 
 
\t \t case 7: System.out.print("Seven "); break; 
 
\t \t case 8: System.out.print("Eight "); break; 
 
\t \t case 9: System.out.print("Nine "); break; 
 
\t \t case 0: System.out.print("Zero "); break; 
 
\t \t default: System.out.print(""); break; 
 
\t \t } 
 
\t } 
 

 
}

+0

如果你可以定義比主以外的功能,在該功能的一個switch語句就足夠了。如果你可以使用循環,對於語句或while語句,可以縮短「提取每個度量」部分。 – 2014-09-19 01:44:46

+0

當你有變量的名稱與複製粘貼代碼,它只是乞求一個循環。 – nmore 2014-09-19 01:51:13

回答

0

呀,你可以簡單地做該操作在for循環中,執行4倍。分區和mod是一致的。類似於

For i = 0; i < 4; i++ 
Number/10%10 
Condition to check number 
Save number in array or print 
+0

哎呀你說沒有迴路 – Xogle 2014-09-19 01:54:56

+1

我只能用if和switch語句。 – 2014-09-19 03:05:30

2

首先,編寫一個方法將單個數字轉換爲單詞。喜歡的東西,

private static String digitToWord(char ch) { 
    switch(ch) {  
    case '0': return "Zero"; 
    case '1': return "One"; 
    case '2': return "Two"; 
    case '3': return "Three"; 
    case '4': return "Four"; 
    case '5': return "Five"; 
    case '6': return "Six"; 
    case '7': return "Seven"; 
    case '8': return "Eight"; 
    case '9': return "Nine"; 
    } 
    return "Unknown (" + ch + ")"; 
} 

然後你就可以得到你intString值。並從String中獲取四個字符。喜歡的東西,

int number = kb.nextInt(); 
String str = String.format("%04d", number); 
StringBuilder sb = new StringBuilder(); 
sb.append(digitToWord(str.charAt(0)).append(' '); 
sb.append(digitToWord(str.charAt(1)).append(' '); 
sb.append(digitToWord(str.charAt(2)).append(' '); 
sb.append(digitToWord(str.charAt(3)); 
System.out.println(sb.toString()); 

或者,

String str = String.format("%04d", kb.nextInt()); 
System.out.printf("%s %s %s %s%n", digitToWord(str.charAt(0)), 
    digitToWord(str.charAt(1)), digitToWord(str.charAt(2)), 
    digitToWord(str.charAt(3))); 
+0

我不能使用數組,只有當和switch語句。 – 2014-09-19 03:04:30

+0

@JeremiahPleasEugeneJohnson編輯刪除陣列。 – 2014-09-19 03:06:11

0

複製粘貼代碼,並與數名應該是紅旗使用循環(或者更多的東西是撥錯)。

private static final String[] DIGIT_NAMES = new String[] {"Zero ", "One ", "Two ", 
    "Three ", "Four ", "Five ", "Six ", "Seven ", "Eight ", "Nine "}; 

    public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 

    int number = 0; 
    do { 
     // ask for a 4 digit integer 
     System.out.println("Enter a 4 digit number: "); 
     try { 
     number = input.nextInt(); 
     } catch (InputMismatchException ignore) { 
     System.out.println("Recieved non integer input"); 
     input.next(); // clear bad input 
     } 
    } while (number < 1000 || number > 9999); 

    String result = ""; 
    while (number != 0) { 
     result = DIGIT_NAMES[number % 10] + result; 
     number = number/10; 
    } 
    System.out.println(result); 
    input.close(); 
    } 
+0

我只能使用if和switch語句,不能使用while或者任何先進的東西。 – 2014-09-19 03:06:51

+0

@JeremiahPleasEugeneJohnson你允許使用方法來封裝你的case語句嗎? – nmore 2014-09-19 05:05:34

1

這可能幫助

public class NumbersInWords { 

    public static void main(String[] args) { 

     String number = "153"; 
     int numLength = number.length(); 
     System.out.println(numLength); 
     String numberToWord = ""; 

     for (int j = 0; j < numLength; j++) { 

      switch (number.charAt(j)) { 

       case '1': { 
        numberToWord = numberToWord + "one"; 
        break; 
       } 
       case '2': { 
        numberToWord = numberToWord + "two"; 
        break; 
       } 
       case '3': { 
        numberToWord = numberToWord + "three"; 
        break; 
       } 
       case '4': { 
        numberToWord = numberToWord + "four"; 
        break; 
       } 
       case '5': { 
        numberToWord = numberToWord + "five"; 
        break; 
       } 
       case '6': { 
        numberToWord = numberToWord + "six"; 
        break; 
       } 
       case '7': { 
        numberToWord = numberToWord + "seven"; 
        break; 
       } 
       case '8': { 
        numberToWord = numberToWord + "eight"; 
        break; 
       } 
       case '9': { 
        numberToWord = numberToWord + "nine"; 
        break; 
       } 
       default: { 
        numberToWord = numberToWord + "zero"; 
       } 
      } 
     } 
     System.out.println(numberToWord); 
    } 
}