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"));
}
添加回答
举报
