2016-03-01 73 views
0

所以我有一個作業,我必須做一個程序來改變羅馬數字的數字。我想使用開關盒,但我不想在1-3999中寫出每一個數字。我被告知,我們可以通過將案件分別更改爲不同號碼的地方來做到這一點,但我不明白這是如何運作的,這是我對那些地方和十個地方的看法,但我如何將它們放在一起?使用開關盒羅馬數字

public class Number { 
public static void main(String[] args) { 
    Scanner scan = new Scanner(System.in); 
    System.out.print("Please enter a number between 1 and 3999: "); 
    int number= scan.nextInt(); 
    String numberString; 

    switch (number%10) 
    { 
     case 1: numberString = "I"; 
       break; 
     case 2: numberString = "II"; 
       break; 
     case 3: numberString = "III"; 
       break; 
     case 4: numberString = "IV"; 
       break; 
     case 5: numberString = "V"; 
       break; 
     case 6: numberString = "VI"; 
       break; 
     case 7: numberString = "VII"; 
       break; 
     case 8: numberString = "VIII"; 
       break; 
     case 9: numberString = "IX"; 
       break; 
     default: numberString = "Invalid nummber"; 
       break; 
    } 

    switch ((number%100)/10) 
    { 
     case 1: numberString = "X"; 
       break; 
     case 2: numberString = "XX"; 
       break; 
     case 3: numberString = "XXX"; 
       break; 
     case 4: numberString = "XL"; 
       break; 
     case 5: numberString = "L"; 
       break; 
     case 6: numberString = "LX"; 
       break; 
     case 7: numberString = "LXX"; 
       break; 
     case 8: numberString = "LXXX"; 
       break; 
     case 9: numberString = "XC"; 
       break; 
     default: numberString = "Invalid nummber"; 
       break; 
    } 
    System.out.println(numberString); 
    } 
} 
+0

你忘了處理零案件。 –

回答

0

當你到你的第二個開關的情況下,你重寫變量numberString。例如,當我給它轉換的數字爲18時,第一個開關情況會將numberString分配給值「VIII」。但是,接下來我轉到第二個開關盒,並用值「X」覆蓋,然後打印。在您的第二個開關盒中,您應該使用

case 1: numberString += "X"; 
      break; 

此外,您應該切換執行切換語句的順序。首先使用開關((數字%100)/ 10),另一秒使用。這將把X的數量放在每個數字的前面。

0

如果我理解你的問題,那麼你可以交換你switch S的順序和使用String串聯。類似的,

String numberString = ""; 
switch ((number%100)/10) 
{ 
    case 1: numberString += "X"; 
      break; 
    case 2: numberString += "XX"; 
      break; 
    case 3: numberString += "XXX"; 
      break; 
    case 4: numberString += "XL"; 
      break; 
    case 5: numberString += "L"; 
      break; 
    case 6: numberString += "LX"; 
      break; 
    case 7: numberString += "LXX"; 
      break; 
    case 8: numberString += "LXXX"; 
      break; 
    case 9: numberString += "XC"; 
      break; 
    default: numberString = "Invalid nummber"; 
      break; 
} 

switch (number%10) 
{ 
    case 1: numberString += "I"; 
      break; 
    case 2: numberString += "II"; 
      break; 
    case 3: numberString += "III"; 
      break; 
    case 4: numberString += "IV"; 
      break; 
    case 5: numberString += "V"; 
      break; 
    case 6: numberString += "VI"; 
      break; 
    case 7: numberString += "VII"; 
      break; 
    case 8: numberString += "VIII"; 
      break; 
    case 9: numberString += "IX"; 
      break; 
    default: numberString = "Invalid nummber"; 
      break; 
} 

System.out.println(numberString); 
+0

噢謝謝我知道我錯過了一些小小的謝謝 – Henry

0

這個解決方案如何不使用開關?它完美的作品。

public class Solution { 
    public String intToRoman(int num) { 
     String result = ""; 

     int M = num/1000; 
     num = num % 1000; 
     for(int i=0; i<M; i++) result += "M"; 

     int CM = num/900; 
     num = num % 900; 
     for(int i=0; i<CM; i++) result += "CM"; 

     int D = num/500; 
     num = num % 500; 
     for(int i=0; i<D; i++) result += "D"; 

     int CD = num/400; 
     num = num % 400; 
     for(int i=0; i<CD; i++) result += "CD"; 

     int C = num/100; 
     for(int i=0; i<C; i++) result += "C"; 
     num = num % 100; 

     int XC = num/90; 
     num = num % 90; 
     for(int i=0; i<XC; i++) result += "XC"; 

     int L = num/50; 
     num = num % 50; 
     for(int i=0; i<L; i++) result += "L"; 

     int XL = num/40; 
     num = num % 40; 
     for(int i=0; i<XL; i++) result += "XL"; 

     int X = num/10; 
     for(int i=0; i<X; i++) result += "X"; 
     num = num % 10; 

     int IX = num/9; 
     num = num % 9; 
     for(int i=0; i<IX; i++) result += "IX"; 

     int V = num/5; 
     num = num % 5; 
     for(int i=0; i<V; i++) result += "V"; 

     int IV = num/4; 
     num = num % 4; 
     for(int i=0; i<IV; i++) result += "IV"; 

     int I = num; 
     for(int i=0; i<I; i++) result += "I"; 

     return result; 
    } 
}