java - 没有收到基于我输入的用户输入的任何输出。

标签 java if-statement do-while

这是我到目前为止的代码,我只是尝试通过输入随机字符来测试它,看看我是否得到正确的 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/

相关文章:

java - javascript 中的多部分表单数据与使用 FileUpload 的 java 服务器

java - 单击按钮后如何添加按钮

c# - 数据读取器中的 while 和 IF

mysql - 在现有字段上使用 If sum 条件更新新列

java - 动态图像制作

C++ : How to reduce conditional statements

c++ - 如何打印数组中的回文数和质数

sql - 经典 ASP while 循环显示错误

Java程序将无法识别哨兵值

java - JUnit 的 hamcrest 库是否有一个 API 来测试公共(public)变量而不是属性