简体   繁体   English

如果输入正确或没有JAVA,如何让用户只知道一次

[英]how to let the user know just once if the input is correct or no JAVA

The whole code works almost like I want to but I don't know how to let the user know the error without finishing de whole for loop, so it repeats the error until the loop is over and also repeats the error until it finds the correct answer.整个代码几乎像我想要的那样工作,但我不知道如何在不完成整个 for 循环的情况下让用户知道错误,因此它会重复错误直到循环结束并重复错误直到找到正确的回答。 If i don't use the for loop i don't know how to iterate inside menu[] to search every match between the elements inside it and the User input... If I write in console "pizza" as an user input it returns:如果我不使用 for 循环,我不知道如何在 menu[] 内部迭代以搜索其中的元素与用户输入之间的每个匹配项...如果我在控制台“pizza”中写入作为用户输入的内容返回:

We have chicken for 10€
We have steak for 15€
We have hamburger for 20€
We have spaghettis for 5€
We have pizza for 12€
What do you want to eat?
pizza
Sorry, we don't have that
Sorry, we don't have that
Sorry, we don't have that
Sorry, we don't have that
You ordered pizza
Want something more? Say YES with 1 and NO with 0

I just want to get returned what I ordered and when i write something wrong then i just want one only advise and not 5!我只想得到我订购的东西,当我写错东西时,我只想要一个建议而不是 5 个! Sorry for my poor english and code knowledge!抱歉我的英语和代码知识很差! Thanks!谢谢!

String[] menu = { "chicken", "steak", "hamburger", "spaghettis", "pizza" };
        int[] price = { 10, 15, 20, 5, 12 };

        for (int i = 0; i < menu.length; i++) {

            System.out.println("We have " + menu[i] + " for " + price[i] + "€");

        }

        boolean wantMore = true;
        int answer = 0;
        int yes = 1;
        int no = 0;

        List<String> Order = new ArrayList<String>();

        List<Integer> TotalPrice = new ArrayList<Integer>();

        Scanner keyboard = new Scanner(System.in);

        System.out.println("What do you want to eat?");

        do {

            String userOrder = keyboard.next().toLowerCase();

            for (int i = 0; i < menu.length; i++) {

                if (userOrder.equals(menu[i])) {

                    Order.add(userOrder);

                    System.out.println("You ordered " + userOrder);

                    TotalPrice.add(price[i]);

                    break;

                } else { 

                    System.out.println("Sorry, we don't have that");

                }

            }

            System.out.println("Want something more? Say YES with 1 and NO with 0");

            answer = keyboard.nextInt();

            if (answer == yes) {

                wantMore = true;

                System.out.println("What else do you want?");

            } else if (answer == no) {

                wantMore = false;

            }

        } while (wantMore);

        int sum = TotalPrice.stream().mapToInt(Integer::intValue).sum();

        System.out.println("Order finished! \nYour order have " + Order);

        System.out.println("The total price is: " + sum);

    }

}

What you want to do is determine if the selected item is within the menu, if it is, order it and ask for more.您要做的是确定所选项目是否在菜单内,如果是,请订购并要求更多。 If it is not within the whole menu, then print the error.如果它不在整个菜单中,则打印错误。 If you change your for loop to如果您将for循环更改for

//flag to keep track of if the item exists in the menu
boolean exists = false;
for (int i = 0; i < menu.length; i++) {
    //set exists to false at the beginning of the loop
    exists = false;
    if (userOrder.equals(menu[i])) {
        Order.add(userOrder);
        System.out.println("You ordered " + userOrder);
        TotalPrice.add(price[i]);
        //if the item exists in the menu, set the flag to true
        exists = true;
    } 
}
//checks if the item was never found in the menu
if (!exists) { 
    System.out.println("Sorry, we don't have that");
}

See if that gives you any better luck with your program.看看这是否能让你的程序有更好的运气。

First go through the loop to find the matching order.首先遍历循环以找到匹配的顺序。 If it is not available, then display the error.如果它不可用,则显示错误。

Try this:尝试这个:

boolean validMenuItem = false;
for (int i = 0; i < menu.length; i++) {

    if (userOrder.equals(menu[i])) {

        Order.add(userOrder);

        System.out.println("You ordered " + userOrder);

        TotalPrice.add(price[i]);

        validMenuItem = true;

        break;

    }

}
if (!validMenuItem) {
    // wrong input
    System.out.println("Sorry, we don't have that");
}

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

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