【问题标题】:Java : while loop not detecting break condition but if block doesJava:while循环未检测到中断条件,但如果块检测到
【发布时间】:2020-02-14 01:34:02
【问题描述】:

只想跳出这个while循环。当我认为条件不会中断时,它不会中断,但它确实会被后续的 if 语句注册。

  String current_Class_Name = "";
  int current_Class_Rating;  

    while(!current_Class_Name.equals("Done")) {

      // * Get class name.
      System.out.println("What class are you rating?");
      current_Class_Name = in.nextLine();
      // *
      // if(current_Class_Name.equals("Done")) {
      // System.out.println("Detected 'Done'");
      // break;
      // }

      // * Get class rating.
      System.out.println("How many plus signs does " + current_Class_Name + " get?");
      current_Class_Rating = Integer.parseInt(in.nextLine());

      // * If user inputs an invalid rating (not a value between 0-5),
      // * keep prompting them until they enter a valid input.
      while(current_Class_Rating > 5 || current_Class_Rating < 0) {
        System.out.println("Sorry, but you can only give a rating of 0-5.");
        System.out.println("How many plus signs does " + current_Class_Name + " get?");
        current_Class_Rating = Integer.parseInt(in.nextLine());
      }

      // * TODO
      // * Add to STRING and INTEGER LISTS.
    }


可能是某种调用顺序问题,因为进入 while 循环时字符串为“空”。 抱歉有任何格式。

感谢所有帮助。

注意:我通常使用 C#,据我记得,while 循环会在完成整个迭代之前尝试检测条件情况。

编辑:if 语句不在那里运行,而只是证明两件事:1)可以检测到中断条件,2)在给定时间中断。

编辑:更新以显示所有考虑的代码。

【问题讨论】:

  • 它确实像你的if 一样工作,我只能假设你在if 之后的循环体中有更多 代码。这将继续评估,直到再次测试循环条件(即当 while 在主体完成后评估时)。
  • 当 current_Class_Name 为“Done”时,您永远不会给 while 条件检查条件,因为当 while 会检查条件。
  • 更新了帖子,考虑了几乎所有代码,并删除了一些误导性/令人困惑的细节。
  • “据我所知,while 循环会在完成整个迭代之前尝试检测条件情况” — 不……它会检查条件 on 进入循环。如果满足条件,则执行整个循环(除非某些原因导致break),然后您返回到循环的顶部,再次测试条件。
  • 完全是@EdwardSuzuki - 或者,您可以使用do { ... } while (condition) 来保证您至少执行循环体一次,并在迭代的结束 处检查条件。

标签: java while-loop break


【解决方案1】:

尝试将实现更改为:

    String current_Class_Name = null;
    Integer current_Class_Rating = null;

    do {
        // * Get class name.
        System.out.println("What class are you rating?");
        current_Class_Name = in.nextLine().trim();


        try {
            // * Get class rating.
            System.out.println(String.format("How many plus signs does '%s' get?",current_Class_Name));
            current_Class_Rating = Integer.parseInt(in.nextLine().trim());
        }catch(NumberFormatException e) {
            current_Class_Rating = null;
        }

        if((current_Class_Rating == null)||(!(current_Class_Rating>=0 && current_Class_Rating <=5))) {
            System.out.println("Invalid rating value! Rating must be integer 0-5!");
            continue;  // skips back to beginning of the loop
        }

        // * TODO
        // * Add to STRING and INTEGER LISTS.

    }while(!current_Class_Name.equalsIgnoreCase("done"));

【讨论】:

    【解决方案2】:

    您的问题似乎是基于一个误解,while 循环只会在条件被重新评估为顶部的false 时终止(而不是在变量被更新的那一刻)。 但是,您可以让提示、分配和评估同时发生。首先,创建一个辅助方法。喜欢,

    private static String promptForClassName(Scanner in) {
        System.out.println("What class are you rating?");
        return in.nextLine();
    }
    

    然后,将它与赋值(作为副作用)评估为赋值的事实一起使用;此外,遵循标准 Java 驼峰式命名约定。喜欢,

    String currentClassName;
    while (!(currentClassName = promptForClassName(in)).equalsIgnoreCase("Done")) {
        String prompt = "How many plus signs does " + currentClassName + " get?";
        // * Get class rating.
        System.out.println(prompt);
        int currentClassRating = Integer.parseInt(in.nextLine());
    
        // * If user inputs an invalid rating (not a value between 0-5),
        // * keep prompting them until they enter a valid input.
        while (currentClassRating > 5 || currentClassRating < 0) {
            System.out.println("Sorry, but you can only give a rating of 0-5.");
            System.out.println(prompt);
            currentClassRating = Integer.parseInt(in.nextLine());
        }
    
        // * TODO
        // * Add to STRING and INTEGER LISTS.
    }
    System.out.println("Detected 'Done'");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-18
      • 2021-12-08
      • 2012-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多