我不知道为什么循环不会停止!爪哇

时间:2019-12-13 19:08:57

标签: java loops for-loop

这是我的第一个疑问,我对此实在是个菜鸟,而且我的英语也不是最好的,所以首先抱歉,如果有一个傻瓜错误。 问题是我想进行一个for循环,以保存来自控制台的每个用户输入,并将其添加到名为“ Order”的列表中。如果用户正确键入订单,则必须执行此操作,并且必须检查它以了解菜单上是否存在该订单。如果不是这样,我们必须让用户知道情况并询问他/她是否想要菜单上有的东西。同样,在存储在列表中的每个输入之后,我们想询问用户他/她是否想订购更多商品,因此如果是,则必须初始化循环,如果不是,则必须退出循环。 我的代码存在的问题是,无论我在控制台中键入什么内容,循环都会从头到尾完成。 错误在哪里? 谢谢!!!


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Fase2_final {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

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

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

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

        }

        int yes = 1;
        int no = 0;

        Scanner yesOrNo = new Scanner(System.in);

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

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

        do {

            Scanner inputOrder = new Scanner(System.in);

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

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

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

                    Order.add(userOrder);

                    System.out.println("You ordered" + Order + "\nSomething more?" + "\nSay YES with 1 and NO with 0");

                } else if (userOrder.equals(menu[j]) == false) {

                    System.out.println("We don't have this on menu!"
                            + "\nDo you want to order another thing that we do have in menu?" + "\nSay YES with 1 and NO with 0");

                } else if (yesOrNo.nextInt() == no) {

                    System.out.println("Order finished!");

                }

            }
        } while (yesOrNo.nextInt() == yes);
    }
}```

3 个答案:

答案 0 :(得分:1)

不需要每次迭代都重新声明Scanner对象,也不需要具有多个将从同一源读取的Scanner对象。另外,也无需打印“我没有找到!”每次比较。

请考虑以下代码块:

.
.
.
// there is no need to re-declare the Scanner object every iteration, or two scanners
Scanner keyboard = new Scanner(System.in);

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

    // we haven't found the item
    boolean found = false;

    // integer option (yes or no)
    int option;

    // do the search
    for (int j = 0; j < menu.length; j++) {
        // do this if found
        if (userOrder.equals(menu[j])) {
            // set flag to indicate we found the item
            found = true;

            // add the order
            Order.add(userOrder);

            // print out, then read the user's option
            System.out.println("You ordered" + userOrder + "\nSomething more?" + "\nSay YES with 1 and NO with 0");
            option = keyboard.nextInt();

            // consume the NL leftover from the nextInt() call
            keyboard.nextLine();

            // break out of the for loop since you already found what you're looking for
            break;
        }
    }

    // check if you didn't find the item
    if (!found) {
        // print out
        System.out.println("We don't have this on menu!"
                            + "\nDo you want to order another thing that we do have in menu?" + "\nSay YES with 1 and NO with 0");

        // read if user wants to continue
        option = keyboard.nextInt();

        // consume the NL leftover from the nextInt() call
        keyboard.nextLine();        
    }
} while (option == yes);

System.out.println("Order finished!");
.
.
.
// close the scanner when you're done
keyboard.close();
.
.
.

答案 1 :(得分:0)

nextInt()方法一次返回当前输入,如果调用两次,则需要第二个输入。它应该看起来像这样:

do {

    Scanner inputOrder = new Scanner(System.in);

    String userOrder = inputOrder.next().toLowerCase();
    int answer = 0;
    for (int j = 0; j < menu.length; j++) {

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

            Order.add(userOrder);

            System.out.println("You ordered" + Order + "\nSomething more?" + "\nSay YES with 1 and NO with 0");

        } else if (userOrder.equals(menu[j]) == false) {

            System.out.println("We don't have this on menu!"
            + "\nDo you want to order another thing that we do have in menu?" + "\nSay YES with 1 and NO with 0");

        } else if ((answer = yesOrNo.nextInt()) == no) {

            System.out.println("Order finished!");

        }

    }
} while (yesOrNo.nextInt() == answer);

我希望我回答了你的问题。

答案 2 :(得分:0)

我已经重构了您的代码。尝试在比较工具中查看。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Fase2_final {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

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

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

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

        }
        int decision = 0;
        int yes = 1;
        int no = 0;

        Scanner yesOrNo = new Scanner(System.in);

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

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

        do {

            Scanner inputOrder = new Scanner(System.in);

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

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

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

                    Order.add(userOrder);

                    System.out.println("You ordered" + Order + "\nSomething more?" + "\nSay YES with 1 and NO with 0");

                } else if (userOrder.equals(menu[j]) == false) {

                    System.out.println("We don't have this on menu!"
                            + "\nDo you want to order another thing that we do have in menu?" + "\nSay YES with 1 and NO with 0");

                } 

            }

            decision = yesOrNo.nextInt();
             if (decision == no) {

                System.out.println("Order finished!");

            }
        } while (decision == yes);
    }
}

完成后始终关闭扫描仪。