这是我到目前为止的代码,我只是试着通过输入随机字符来测试它,看看我是否得到了正确的if语句,(也输入了quit)。
然而,输入输入后没有任何反应,原因是什么原因?
提前谢谢你。
public static void main(String[] args) {
//==============================================================================
// Opening Message.
Scanner scan = new Scanner(System.in);
System.out.print("Welcome to the Theatre Booking System. (QUIT to exit)"
+ "\nWould you like to purchase tickets or list available seats?"
+ "(Purchase/List/Help)");
scan.nextLine();
String answer;
answer = scan.nextLine();
int count = 0;
//==============================================================================
// Nested if statement.
// First if statement.
if (answer.equalsIgnoreCase("purchase")) {
// Code for purchase
} else {
if (answer.equalsIgnoreCase("list")) {
// Code for list
} else {
if (answer.equalsIgnoreCase("help")) {
// Code for help
} else {
if (answer.equalsIgnoreCase("quit")) {
System.exit(-1);
} else {
do {
System.out.print("Sorry, incorrect input please enter"
+ " a valid input (Purchase/List/Help or QUIT to exit");
scan.nextLine();
count++;
} while (!answer.equalsIgnoreCase("purchase")
|| !answer.equalsIgnoreCase("list")
|| !answer.equalsIgnoreCase("quit")
|| !answer.equalsIgnoreCase("help"));
}
}
}
}
}
}
答案 0 :(得分:1)
您有两个scan.nextLine()
语句,并使用第二个语句进行输入。因此,您需要先按“Enter”然后按输入,然后再按“Enter”。
同时避免嵌套 - 如果,您可以直接使用具有多个if
块的else if