简体   繁体   English

为什么这个循环会回到开始?

[英]Why does this loop go back to the beginning?

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

class SteppingStone4_Loops {

    public static void main(String[] args) {
       Scanner scnr = new Scanner(System.in);
       String recipeName = "";
       ArrayList<String> ingredientList = new ArrayList();
       String newIngredient = "";
       boolean addMoreIngredients = true;

       System.out.println("Please enter the recipe name: ");
       recipeName = scnr.nextLine();


       do {           
           System.out.println("Would you like to enter an ingredient: (y or n)");
           String reply = scnr.next().toLowerCase();

           /**The code should check the reply:
            *   "y" --> prompt for the ingredient and add it to the ingredient list;
            *   "n" --> break out of the loop;  
            *   anything else --> prompt for a "y" or "n"
            */

          while (true) {
             if (reply.equals("y")) {
               System.out.println("Enter ingredient name: "); 
               newIngredient = scnr.next();   
               ingredientList.add(newIngredient);
             break;
           }
             else if (reply.equals("n")) {
                System.out.println("Goodbye!");
                break;
             }

            else  
               break;
           }


            } while (addMoreIngredients);
           for (int i = 0; i < ingredientList.size(); i++) {
           String ingredient = ingredientList.get(i);
           System.out.println(ingredient);
       }
    }
}

When ran, the program returns this:运行时,程序返回:

Please enter the recipe name:请输入配方名称:
Polenta玉米粥
Would you like to enter an ingredient: (y or n)您想输入一种成分吗:(y 或 n)
y
Enter ingredient name:输入成分名称:
Salt
Would you like to enter an ingredient: (y or n)您想输入一种成分吗:(y 或 n)
n n
Would you like to enter an ingredient: (y or n)您想输入一种成分吗:(y 或 n)
5 5
Would you like to enter an ingredient: (y or n)您想输入一种成分吗:(y 或 n)

Why doesn't it break when reply = n?为什么当回复 = n 时它不会中断? Why does it go back to the "Would you like to enter an ingredient"?为什么会回到“您想输入一种成分”吗? Can someone pinpoint my mistake or perhaps suggest a different way?有人可以指出我的错误或提出不同的建议吗? Thanks谢谢

Please note, you have 2 while loops请注意,您有 2 个while循环

  else if (reply.equals("n")) {
            System.out.println("Goodbye!");
            break;
         }

The above condition will break out from inner while loop and outer while loop will still continue and that's why you are getting output "Would you like to enter an ingredient", which part of outer loop.上述条件将从内部while循环和外部while循环中断,这就是为什么你会得到输出“你想输入一种成分”,这是外部循环的哪一部分。

Since, in outer loop, you have below condition, which is set true and never seems to be changed.因为,在外循环中,您有以下条件,该条件设置为true并且似乎永远不会改变。

while (addMoreIngredients)

So, before breaking out from inner loop, you could change addMoreIngredients to false因此,在从内循环中断之前,您可以将addMoreIngredients更改为false

  else if (reply.equals("n")) {
        System.out.println("Goodbye!");
        addMoreIngredients = false;
        break;
     }

Just to let you know, you could also use label break instead.只是为了让您知道,您也可以改用标签分隔符。

while (addMoreIngredients);

This line always returns true.此行始终返回 true。 So your do loop keeps on looping.所以你的 do 循环继续循环。

I suggest setting a flag from inner loop for outerloop as well.我建议也从内循环为外循环设置一个标志。

--edit----- lets get rid of inner loop, i think it should work fine. --edit----- 让我们摆脱内部循环,我认为它应该可以正常工作。 PS: right now i don't have access to a JRE. PS:现在我无法访问 JRE。 But i think it should work但我认为它应该工作

do {           
           System.out.println("Would you like to enter an ingredient: (y or n)");
           String reply = scnr.next().toLowerCase();

             if (reply.equals("y")) {
               System.out.println("Enter ingredient name: "); 
               newIngredient = scnr.next();   
               ingredientList.add(newIngredient);
               continue;
           }
             else if (reply.equals("n")) {
                System.out.println("Goodbye!");
                break;
             }

            else {
            System.out.println("Invalid option!");
            continue;
            }   
 } while (addMoreIngredients);

Please check that you have nested loops and break statement in inner loop only break out from inner loop and you apply the second condition of the do-while loop on addMoreIngredients = true请检查内部循环中是否有嵌套循环和 break 语句仅从内部循环中断,并且在addMoreIngredients = trueaddMoreIngredients = true do-while 循环的第二个条件

break statment only break out the inner loop. break 语句只中断内部循环。

so it always runs because addMoreIngredients always true.所以它总是运行,因为addMoreIngredients总是正确的。

make addMoreIngredients is false使 addMoreIngredients 为假

 else if (reply.equals("n")) {
            System.out.println("Goodbye!");
            addMoreIngredients = false;
            break;
         }

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

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