所以我有一個作業,我必須做一個程序來改變羅馬數字的數字。我想使用開關盒,但我不想在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);
}
}
你忘了處理零案件。 –