我有這個課程的作業,我正在回收,我遇到的問題是我過度思考解決方案。我必須創建一個將四位數字轉換爲單詞的程序。從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 }
}
如果你可以定義比主以外的功能,在該功能的一個switch語句就足夠了。如果你可以使用循環,對於語句或while語句,可以縮短「提取每個度量」部分。 – 2014-09-19 01:44:46
當你有變量的名稱與複製粘貼代碼,它只是乞求一個循環。 – nmore 2014-09-19 01:51:13