java:while循环未检测到中断条件,但如果块检测到

rnmwe5a2  于 2021-07-12  发布在  Java
关注(0)|答案(2)|浏览(464)

只是想打破这个循环。条件并没有在我假定它会中断时中断,但它确实会被随后的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循环将尝试在完成整个迭代之前检测条件情况。
edit:if语句不是用来运行的,而是用来证明两件事:1)中断条件可以被检测到,2)在给定的时间中断。
编辑:更新以显示考虑中的所有代码。

ddrv8njm

ddrv8njm1#

尝试将实现更改为:

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

ix0qys7i2#

你的问题似乎是建立在一个误解的基础上 while 循环仅在条件重新计算为 false 在顶部(不是在变量被更新的瞬间)。但是,您可以将其设置为提示、分配和评估同时发生。首先,创建一个helper方法。比如,

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

然后,将它与赋值(作为副作用)计算为赋值的事实一起使用;另外,请遵循标准的javacamel大小写命名约定。比如,

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

相关问题