简体   繁体   English

Java:while 循环未检测到中断条件,但 if 块检测到

[英]Java : while loop not detecting break condition but if block does

just want to break out of this while loop.只想跳出这个 while 循环。 Condition doesn't break when I assumed it will, but it does get registered by the subsequent if statement.当我假设条件不会中断时,条件不会中断,但它确实会被后续的 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.
    }


Possibly some kind of call order problem as the String is "empty" upon entering the while loop.可能是某种调用顺序问题,因为 String 在进入 while 循环时为“空”。 Sorry for any formatting.对不起任何格式。

Thanks for any and all help.感谢您提供的所有帮助。

NOTE: I typically use C# and as far as I remember, a while loop will try and detect the condition case before completing an entire iteration.注意:我通常使用 C#,据我所知,while 循环会在完成整个迭代之前尝试并检测条件情况。

EDIT: The if statement is not there to run, but to just prove two things: 1) that the break condition can be detected and 2) to break out at a given time.编辑:if 语句不是用来运行的,而只是为了证明两件事:1)可以检测到中断条件,2)在给定时间中断。

EDIT: Updated to show all code in consideration.编辑:更新以显示考虑中的所有代码。

Try changing the implementation to:尝试将实现更改为:

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

Your question appears to have been based on a misconception, the while loop will only terminate when the condition is re-evaluated to be false at the top (not on the instant of the variable being updated).您的问题似乎是基于一个误解, while循环只会在条件在顶部被重新评估为false时终止(而不是在变量被更新的瞬间)。 However , you can make it so the prompt, the assignment and the evaluation happen at once.但是,您可以使提示、分配和评估同时发生。 First, create a helper method.首先,创建一个辅助方法。 Like,像,

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

Then, use it with the fact that assignment (as a side-effect) evaluates to the assigned value;然后,将它与赋值(作为副作用)评估为赋值的事实一起使用; also, please follow standard Java camel case naming conventions.另外,遵循标准 Java 驼峰命名约定。 Like,像,

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

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

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