简体   繁体   English

Java else if 和 else 在 while 循环下使用 Scanner 时不起作用

[英]Java else if and else not working when using Scanner under while loop

Whenever I run the following code, the if statement that asks for yes/y works when run, however, the else if and else statements don't work.每当我运行以下代码时,要求 yes/y 的 if 语句在运行时起作用,但是,else if 和 else 语句不起作用。 When I try typing in n, no, or anything else, it just stays there.当我尝试输入 n、no 或其他任何内容时,它只是停留在那里。 No reaction, no crashes, just sticks there.没有反应,没有崩溃,只是坚持在那里。 Anyways here's the code:无论如何这里的代码:

public static void main(String[] args) {
    String[] items;
    double[] cost;
    int[] quantity;
    double[] totalItemCost;
    int amountOfItems;
    double totalCost = 0;
    Scanner input = new Scanner(System.in);

    while(true) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        System.out.println("Welcome to The TC corner market, this is an automated cashier program to check out your items.\n");
        System.out.println("May we proceed? y/n");
        if (input.next().equals("y") || input.next().equals("yes")) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            System.out.println("Okay, let's proceed.");
            try {
                Thread.sleep(200);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            System.out.println("How many items will you be checking out?");
            amountOfItems = input.nextInt();
            items = new String[amountOfItems];
            cost = new double[amountOfItems];
            quantity = new int[amountOfItems];
            totalItemCost = new double[amountOfItems];

            try {
                Thread.sleep(200);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            for (int i = 0; i < amountOfItems; i++) {
                System.out.println("\nWhat is the name of the item?");
                items[i] = input.next();
                System.out.println("What is the cost of the item?");
                cost[i] = input.nextDouble();
                System.out.println("What is the quantity of the item you are purchasing?");
                quantity[i] = input.nextInt();
                totalItemCost[i] = cost[i] * quantity[i];
                System.out.printf("Item:\t\tCost:\t\tQuantity:\t\tTotal Item Cost:\n%s\t\t%.2f\t\t%d\t\t\t\t%.2f", items[i], cost[i], quantity[i], totalItemCost[i]);
                totalCost = totalCost + cost[i];
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            System.out.println("\n\nYour Checkout Cart:\n");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            System.out.println("Items:\t\tCost:\t\tQuantity:\t\tTotal Item Cost:\n");
            for (int j = 0; j < amountOfItems; j++) {
                System.out.printf("%s\t\t%.2f\t\t%d\t\t\t\t%.2f\n", items[j], cost[j], quantity[j], totalItemCost[j]);
            }
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            System.out.printf("\nYour total: %.2f", totalCost);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            System.out.println("\n\nThank you for checking out with The TC corner market! This program will restart in 10 seconds.");
            try {
                Thread.sleep(10000);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        } else if (input.next().equals("n") || input.next().equals("no")) {
            System.out.println("In that case, please proceed to return your items back to the store");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            break;
        } else {
            try {
                Thread.sleep(200);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            System.out.println("Invalid Answer");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            break;

        }
    }
}

I tried doing many things, like changing it from else if to regular if, adding the break (which I now have in my code I am posting above), and just completely removing the else statement and turning the else if to else.我尝试做很多事情,比如将它从 else if 更改为常规 if,添加中断(我现在在上面发布的代码中已经有了),然后完全删除 else 语句并将 else if 转换为 else。 None of them worked though.他们都没有工作。

You need to store the value of input.next() in a variable before your if condition.您需要在if条件之前将input.next()的值存储在变量中。

Something like this:像这样的东西:

String next = input.next();

And replace all subsequent input.next() with next .并将所有后续input.next()替换为next


Explanation:解释:

The reason it's not working is that with each input.next() it waits for a new string input.它不工作的原因是每个input.next()它等待一个新的字符串输入。

Let's say you input: "n"假设您输入: "n"

In the first if condition it will be compared with "y" which it won't match, the next input.next() will try to retake input, that's why it isn't showing anything because it's waiting for your input.在第一个if条件中,它将与不匹配的"y"进行比较,下一个input.next()将尝试重新获取输入,这就是为什么它不显示任何内容的原因,因为它正在等待您的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM