public class child {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int AGE;
System.out.println("Enter Child's number(s) of Days: ");
AGE = scan.nextInt();
if (AGE == 0 || AGE == 1){
System.out.println("Classification: New Born");
}
else if (AGE >= 2 && AGE <= 10){
System.out.println("Classification: Infant");
}
else if (AGE > 9 && AGE < 19){
System.out.println("Classification: New Born");
}
else if (AGE > 17 && AGE < 37){
System.out.println("Classification: TODDLER");
}
else if (AGE > 36 && AGE < 120) {
System.out.println("Classification: KID");
}
else{
System.out.println("Classification: Out of Range");
System.out.println("Enter a number again:");
AGE = scan.nextInt();
if (AGE == 0 || AGE == 1){
System.out.println("Classification: New Born");
}
else if (AGE >= 2 && AGE <= 10){
System.out.println("Classification: Infant");
}
else if (AGE > 9 && AGE < 19){
System.out.println("Classification: New Born");
}
else if (AGE > 17 && AGE < 37){
System.out.println("Classification: TODDLER");
}
else if (AGE > 36 && AGE < 120) {
System.out.println("Classification: KID");
}
else{
System.out.println("Classification: Out of Range");
System.out.println("Enter a number again:");
AGE = scan.nextInt();
}
}
}
}
因此,我根據他們的天數對孩子進行了分類。如何讓程序不斷提示用戶?
如何簡化此代碼,因爲如果用戶輸入超出範圍的數字,程序將提示用戶輸入一個值,但如果用戶再次輸入一個超出範圍的數字現在將停止提示用戶輸入另一個號碼,因爲如果我把另一個條件結構放在其他地方,它會使我的代碼更長,所以我的問題是如何縮短我的代碼,但如果輸入的號碼程序將繼續提示用戶超出範圍。
樣本輸出 輸入孩子的天數(S):分類:超出範圍 再次輸入一個數字: //程序會提示用戶重新 -1 //但現在的程序現在不會打印出的範圍 /我如何使程序提示用戶再次輸入一個數字,讓我 短節目/
使用'while'循環。 –
使用'while'循環 –