简体   繁体   English

用户输入错误时如何循环代码

[英]How to loop the code when user entered wrong input

do {
    id = "";
    quantity = 0;
    System.out.print("Enter id : ");
    id = sc.nextLine();
    for (int i = 0; i < food.length; i++){
        if ((id.toUpperCase()).equals(food[i].getMenuID())) {
            correctInput = true;
            do{
                System.out.print("Enter quantity : ");
                quantity = sc.nextInt();
                if (quantity < 1)
                System.out.println("\nInvalid quantity! Please enter again\n");
        else {
                total = quantity * food[i].getPrice();
                System.out.print(total);
        }
            } while (quantity < 1);
                }
        else if (!((id.toUpperCase()).equals(food[i].getMenuID())))
            System.out.println("\nInvalid ID! Please enter again!\n");
        }
} while (correctInput = false);

I want to loop from "Enter id" if user entered a wrong ID , but I just can't figure out how to do it.如果用户输入了错误的ID ,我想从“输入 ID”循环,但我不知道该怎么做。

I have tried using do while loop by setting boolean to false when wrong input but it is not working.我曾尝试通过在错误输入时将boolean设置为 false 来使用do while循环,但它不起作用。

How can I achieve that with this code?我如何使用此代码实现这一目标?

I am using JCreator.我正在使用 JCreator。

在 do while 代码之前确保你的正确输入变量是假的而且它是correctInput == false而不是correctInput = false

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

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