public static void add2q() {
System.out.print("Age:");
age = sc.nextInt();
if (!(age >= 3 && age <= 80)) {
System.out.println("Sorry you must be between the age of 3 to 80");
} else if (age >= 3 && age <= 5) {
while (true) {
System.out.println("Need to be accompanied by an adult above 16\nHave an adult to accompany?y/n");
String yn = sc.nextLine();
if (yn.equals("y")) {
System.out.print("Child's Name: ");
name = sc.nextLine();
names.add(name);
price();
addAdult();
break;
} else if (yn.equals("n")) {
while (true) {
System.out.println("Sorry you cannot take this ride");
add2q();
break;
}
} else {
System.out.println("Invalid output"); //funny problem here
}
}
} else {
System.out.print("Name: ");
name = sc.nextLine();
names.add(name);
price();
}
}
當用戶輸入3或4或5時,它將打印「需要......」以及「無效的輸出語句」,但我不想要最後的聲明。我如何防止顯示最後一條語句?不需要時打印一個else語句
在「3或4或5」之後鍵入什麼? –
那麼你的代碼在做什麼。如果年齡在(3:5)的範圍內,但下一個輸入('yn')不是「」y「或」n「,它將輸出無效輸出。只是更新你的條件,如果你不想要 – AxelH
假設鍵入y/n,但在我鍵入之前,它會打印「無效的輸入」@Coldspeed –