这是我到目前为止的代码,我只是尝试通过输入随机字符来测试它,看看我是否得到正确的 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"));
}
}
}
}
}
}
最佳答案
您有两个 scan.nextLine()
语句,并且使用第二个语句进行输入。所以你需要先按“Enter”,然后输入,然后再按“Enter”。
同时避免嵌套if,可以直接将同一个if
与多个else if
block 一起使用
关于java - 没有收到基于我输入的用户输入的任何输出。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13043139/