Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.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
为什么这个循环要回到开始? import java.util.ArrayList; 导入java.util.Scanner; 类steppingstone 4_循环{ 公共静态void main(字符串[]args){ 扫描仪scnr=新扫描仪(System.in); 字符串recipeName=“”; ArrayList ingredientList=新建ArrayList(); 字符串newcomponent=“”; 布尔值addmore=true; System.out.println(“请输入配方名称:”); recipeName=scnr.nextLine(); 做{ System.out.println(“您想输入成分:(y还是n)”; 字符串reply=scnr.next().toLowerCase(); /**代码应检查回复: *“y”-->提示输入成分并将其添加到成分列表中; *“n”-->打破循环; *任何其他-->提示输入“y”或“n”的内容 */ while(true){ 如果(回复等于(“y”)){ System.out.println(“输入成分名称:”); newcomponent=snr.next(); IngreditList.add(新成分); 打破 } else if(回复等于(“n”)){ System.out.println(“再见!”); 打破 } 其他的 打破 } }while(添加更多成分); 对于(int i=0;i_Java_If Statement_Do While - Fatal编程技术网

为什么这个循环要回到开始? import java.util.ArrayList; 导入java.util.Scanner; 类steppingstone 4_循环{ 公共静态void main(字符串[]args){ 扫描仪scnr=新扫描仪(System.in); 字符串recipeName=“”; ArrayList ingredientList=新建ArrayList(); 字符串newcomponent=“”; 布尔值addmore=true; System.out.println(“请输入配方名称:”); recipeName=scnr.nextLine(); 做{ System.out.println(“您想输入成分:(y还是n)”; 字符串reply=scnr.next().toLowerCase(); /**代码应检查回复: *“y”-->提示输入成分并将其添加到成分列表中; *“n”-->打破循环; *任何其他-->提示输入“y”或“n”的内容 */ while(true){ 如果(回复等于(“y”)){ System.out.println(“输入成分名称:”); newcomponent=snr.next(); IngreditList.add(新成分); 打破 } else if(回复等于(“n”)){ System.out.println(“再见!”); 打破 } 其他的 打破 } }while(添加更多成分); 对于(int i=0;i

为什么这个循环要回到开始? import java.util.ArrayList; 导入java.util.Scanner; 类steppingstone 4_循环{ 公共静态void main(字符串[]args){ 扫描仪scnr=新扫描仪(System.in); 字符串recipeName=“”; ArrayList ingredientList=新建ArrayList(); 字符串newcomponent=“”; 布尔值addmore=true; System.out.println(“请输入配方名称:”); recipeName=scnr.nextLine(); 做{ System.out.println(“您想输入成分:(y还是n)”; 字符串reply=scnr.next().toLowerCase(); /**代码应检查回复: *“y”-->提示输入成分并将其添加到成分列表中; *“n”-->打破循环; *任何其他-->提示输入“y”或“n”的内容 */ while(true){ 如果(回复等于(“y”)){ System.out.println(“输入成分名称:”); newcomponent=snr.next(); IngreditList.add(新成分); 打破 } else if(回复等于(“n”)){ System.out.println(“再见!”); 打破 } 其他的 打破 } }while(添加更多成分); 对于(int i=0;i,java,if-statement,do-while,Java,If Statement,Do While,运行时,程序返回以下内容: 请输入配方名称: 玉米粥 是否要输入一种成分:(y或n) y 输入成分名称: 盐 是否要输入一种成分:(y或n) n 是否要输入一种成分:(y或n) 5 是否要输入一种成分:(y或n) 为什么当reply=n时它不中断?为什么要回到“您想输入一种配料吗”?有人能指出我的错误,或者提出不同的方法吗?谢谢请注意,您有两个while循环 import java.util.ArrayList; import java.util.Scanner; class Stepping

运行时,程序返回以下内容:

请输入配方名称:
玉米粥
是否要输入一种成分:(y或n)
y
输入成分名称:

是否要输入一种成分:(y或n)
n
是否要输入一种成分:(y或n)
5
是否要输入一种成分:(y或n)


为什么当reply=n时它不中断?为什么要回到“您想输入一种配料吗”?有人能指出我的错误,或者提出不同的方法吗?谢谢

请注意,您有两个
while
循环

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);
       }
    }
}
上述条件将从内部while循环中断,而外部while循环仍将继续,这就是为什么您会得到输出“您想输入一种成分吗”,这是外部循环的一部分

因为,在外循环中,您有下面的条件,它被设置为
true
,并且似乎永远不会改变

  else if (reply.equals("n")) {
            System.out.println("Goodbye!");
            break;
         }
所以,在从内部循环中断之前,您可以将
addMoreComponents
更改为
false

while (addMoreIngredients)
只是想让你知道,你也可以用标签断裂代替

  else if (reply.equals("n")) {
        System.out.println("Goodbye!");
        addMoreIngredients = false;
        break;
     }
这一行总是返回true。所以你的do循环继续循环

我建议也从内部循环为外部循环设置一个标志

--编辑----- 让我们去掉内环,我认为它应该可以正常工作。 附言:现在我没有访问JRE的权限。但我认为它应该起作用

while (addMoreIngredients);

请检查您是否在内部循环中有嵌套循环和break语句,仅从内部循环中断,并且您应用了do while循环开启的第二个条件
addMoreComponents=true

断开状态仅断开内环

所以它总是运行,因为
addMoreComponents
总是正确的


制造更多的成分是假的

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);

你有一个
中断声明是否
reply.equals(“y”)
reply.equals(“n”)
。你为什么期望他们表现不同?想想看,你的内部循环从来没有真正循环过。如果你摆脱它,
break将跳出外部的。如何设置这样的标志?我认为break命令会导致循环结束。@tulcae65我已经修改了我的立场,并建议了一个我认为应该符合您要求的代码。在您的案例中不需要两个循环。这样,您的代码应该更加优化。请原谅代码的格式。我没有带任何Java ide,也没有合适的知识来调整它。非常感谢你的建议!我运行了代码,它似乎工作正常。NetBeans说,这两个选项将继续;命令是不必要的,但我会把它们放在那里。是的,我只是把它放在那里,因为它对初学者来说是有意义的,如果你觉得答案可以接受,请把它标记为answeredI做了更改,它似乎有帮助,但现在,当我用不同的答案运行程序时,我得到的是,当我选择“n”时:请输入配方名称:polenta您是否要输入配料:(y或n)y输入配料名称:配料您是否要输入配料:(y或n)5您是否要输入配料:(y或n)n再见!配料构建成功(总时间:19秒)不明白为什么选择“n”时最后一个输入(配料)会跟在“再见!”后面…如果不使AddMoreComponents=false,则break语句将从内部while循环中出现。但要走出外循环,它需要检查addMoreComponents变量,该变量始终为真。这是代码的当前版本(基于到目前为止发布的建议):else if(reply.equals(“n”){System.out.println(“再见”);addmoreingents=false;break;}
 else if (reply.equals("n")) {
            System.out.println("Goodbye!");
            addMoreIngredients = false;
            break;
         }