1 回答

TA貢獻1810條經驗 獲得超4個贊
1)您的條件是多余的。您可以使用簡單,因為輸入只能在該范圍內進行。 僅當您要檢查兩個或多個范圍時才有意義,例如if - else ifif - elseelse if
if(input > 0 && input < 3999){
...
}
else if (input > 4000 && input < 8000){
...
}
else {
...
}
2) 您不需要開關塊,而是在 while 條件下使用用戶輸入,因為您希望在用戶輸入為 Y/y 時繼續循環,即while(userChoice.equals("Y"))
3) 使用循環,因為您希望應用程序至少按時運行do - while
public static void main(String[] args) {
System.out.println("Welcome to my integer Roman numeral conversion program");
System.out.println("------------------------------------------------------");
System.out.println(" ");
Scanner in = new Scanner (System.in);
String choice;
do{
System.out.print("Enter an integer in the range 1-3999 (both inclusive): ");
int input = in.nextInt();
if(input > 0 && input < 3999){
System.out.println(Conversion.Convert(input));
}
else{
System.out.println("Sorry, this number is outside the range.");
}
System.out.println("Do you want to try again? Press Y for yes and N for no: ");
choice = in.next();
}while(choice.equals("Y") || choice.equals("y"));
}
添加回答
舉報