0

这是我到目前为止的代码,我只是尝试通过输入随机字符来测试它,看看我是否得到了正确的 if 语句(也输入了退出)。

但是,在我输入我的输入后什么都没有发生,有什么原因吗?

先感谢您。

 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"));

                    }
                }
            }
        }
    }
}
4

1 回答 1

1

您有两个scan.nextLine()语句,您使用第二个语句作为输入。因此,您需要先按“Enter”,然后再按“Enter”输入。

还要避免嵌套if,可以直接用同一个ifelse if

于 2012-10-24T04:46:59.887 回答