Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在while循环下使用Scanner时,Java else if和else不工作_Java_Intellij Idea - Fatal编程技术网

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

在while循环下使用Scanner时,Java else if和else不工作,java,intellij-idea,Java,Intellij Idea,每当我运行以下代码时,请求yes/y的if语句在运行时起作用,而else if和else语句则不起作用。当我尝试输入n、no或其他任何东西时,它就停留在那里。没有反应,没有碰撞,只是停留在那里。无论如何,代码如下: public static void main(String[] args) { String[] items; double[] cost; int[] quantity; double[] totalItemCost; int amount

每当我运行以下代码时,请求yes/y的if语句在运行时起作用,而else if和else语句则不起作用。当我尝试输入n、no或其他任何东西时,它就停留在那里。没有反应,没有碰撞,只是停留在那里。无论如何,代码如下:

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;

        }
    }
}
publicstaticvoidmain(字符串[]args){
字符串[]项;
双[]成本;
int[]数量;
双重成本;
国际货币基金组织;
双重总成本=0;
扫描仪输入=新扫描仪(System.in);
while(true){
试一试{
睡眠(500);
}捕获(中断异常例外){
Thread.currentThread().interrupt();
}
System.out.println(“欢迎来到TC角落市场,这是一个自动出纳程序,用于检查您的物品。\n”);
系统输出打印(“我们可以继续吗?是/否”);
if(input.next().equals(“y”)| input.next().equals(“yes”)){
试一试{
睡眠(500);
}捕获(中断异常例外){
Thread.currentThread().interrupt();
}
System.out.println(“好的,让我们继续。”);
试一试{
睡眠(200);
}捕获(中断异常例外){
Thread.currentThread().interrupt();
}
System.out.println(“您将签出多少项?”);
amountOfItems=input.nextInt();
items=新字符串[amountOfItems];
成本=新的双倍[金额];
数量=新整数[数量];
totalItemCost=新的双倍[AmountFitems];
试一试{
睡眠(200);
}捕获(中断异常例外){
Thread.currentThread().interrupt();
}
对于(int i=0;i

我试过做很多事情,比如将它从elseif改为常规if,添加中断(我现在在上面发布的代码中有中断),完全删除else语句并将elseif改为else。但是它们都不起作用。

您需要在
if
条件之前将
input.next()
的值存储在变量中

大概是这样的:

String next = input.next();
并将所有后续的
input.next()
替换为
next


说明:

它不工作的原因是,对于每个
输入。next()
它都会等待新的字符串输入

假设您输入:
“n”


在第一个
if
条件中,它将与它不匹配的
“y”
进行比较,下一个
input.next()
将尝试重新输入,这就是为什么它没有显示任何内容,因为它正在等待您的输入。

您在if语句
input.next()中的错误等于(“y”)| input.next().equals(“yes”)
。。。对于中断程序,您应该在控制台中键入
n
。如果您在第一次检查后键入console
n
,则
input.next().equals(“y”)
扫描仪将为空-这就是为什么您不这样做的原因