1 回答

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
1)您的條件是多余的。您可以使用簡單,因?yàn)檩斎胫荒茉谠摲秶鷥?nèi)進(jìn)行。 僅當(dāng)您要檢查兩個(gè)或多個(gè)范圍時(shí)才有意義,例如if - else ifif - elseelse if
if(input > 0 && input < 3999){
...
}
else if (input > 4000 && input < 8000){
...
}
else {
...
}
2) 您不需要開關(guān)塊,而是在 while 條件下使用用戶輸入,因?yàn)槟M谟脩糨斎霝?Y/y 時(shí)繼續(xù)循環(huán),即while(userChoice.equals("Y"))
3) 使用循環(huán),因?yàn)槟M麘?yīng)用程序至少按時(shí)運(yùn)行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"));
}
添加回答
舉報(bào)