【问题标题】:Infinite while loop involving boolean variable in JavaJava中涉及布尔变量的无限while循环
【发布时间】:2014-05-17 10:54:07
【问题描述】:

这是我的第一篇文章,如果我未能在本网站上遵循正确的格式/约定等,敬请见谅。我只编程了几个星期。

无论如何,我正在使用布尔变量和 while 循环编写一个基本的 Java 程序。该代码至少可以说非常“笨拙”,并且肯定可以变得更加优雅(尽管不是我有限的技能,所以再次道歉)。 如果布尔变量为真,则代码将正常运行。但是,如果变量为假,代码会进入一个 while 循环,即使输入了应该使语句为真的输入,循环也会继续进行。 知道是什么导致了这个无限循环。我几乎可以肯定这是基本的东西,但我似乎无法弄清楚。这是下面的代码。谢谢!

import java.util.Scanner;
import java.util.Random;
import static java.lang.System.out;
import static java.lang.System.in;

public class MB1 {
    public static void main(String args[]){
        char a, b, c, d;
        Scanner myScanner = new Scanner(in);
        boolean secondBoolean;

        out.println("Let's get started! Type in your 4-digit code:");
        a = myScanner.findWithinHorizon(".", 0).charAt(0);
        b = myScanner.findWithinHorizon(".", 0).charAt(0);
        c = myScanner.findWithinHorizon(".", 0).charAt(0);
        d = myScanner.findWithinHorizon(".", 0).charAt(0);

        secondBoolean = ((a == '1'|| a == '2'|| a == '3' || a == '4' || a == '5' || a == '6' || a == '7')
            && (b == '1' || b == '2'|| b == '3' || b == '4' || b == '5' || b == '6' || b == '7')
            && (c == '1' || c == '2'|| c == '3' || c == '4' || c == '5' || c == '6' || c == '7')
            && (d == '1' || d == '2'|| d == '3' || d == '4' || d == '5' || d == '6' || d == '7'));

            while (secondBoolean == false) {
            out.println("The code you typed is not valid. Please type a different code:");
            a = myScanner.findWithinHorizon(".", 0).charAt(0);
            b = myScanner.findWithinHorizon(".", 0).charAt(0);
            c = myScanner.findWithinHorizon(".", 0).charAt(0);
            d = myScanner.findWithinHorizon(".", 0).charAt(0);  
            out.print(a);out.print(b);out.print(c);out.print(d);
            } 
            if (secondBoolean == true){
                out.println('0');
            }
        }
    }

【问题讨论】:

  • 旁注:这个布尔表达式真的很难看,很难阅读。我会把它改成secondBoolean = a >= '1' && a <= '7' || b >= '1' && b <= '7' || c >= '1' && c <= '7' || d >= '1' && d <= '7'
  • 你的代码缩进关闭了。
  • 你的无限循环问题已经有足够的答案了,我只是建议使用“do while”循环来减少代码的冗余(重复输入和布尔赋值部分)

标签: java while-loop boolean infinite-loop


【解决方案1】:

是的,当secondBoolean 为假时,您进入循环。 您将在此循环中循环,直到 secondBoolean 设置为 true,因此您必须在 while 循环中更新它。

另一方面,也许你的代码可以像这样整理一下;

List<String> nums = Arrays.asList("1", "2", "3", "4", "5", "6", "7");
secondBoolean=(nums.contains(a) && 
               nums.contains(b) && 
               nums.contains(c) && 
               nums.contains(d));

我只是自己开始java,但这看起来有点整洁! 希望对您有所帮助。

【讨论】:

    【解决方案2】:

    您可以尝试仅将用户的输入读取为字符串并用“。”将其拆分

    import java.util.Scanner;
    
    import static java.lang.System.in;
    import static java.lang.System.out;
    
    public class MB1 {
    
        public static void main(String args[]) {
            char a, b, c, d;
            Scanner myScanner = new Scanner(in);
            boolean secondBoolean;
    
            out.println("Let's get started! Type in your 4-digit code:");
            String strIn = myScanner.nextLine();
    
            // Split input by "."
            String[] strAry = strIn.split("\\.");
    
            // create char array of equal length to String array
            char[] chrAry = new char[strAry.length];
    
            // convert strings to chars
            for (int i = 0; i < strAry.length; i++) {
                chrAry[i] = strAry[i].charAt(0);
            }
    
            // (unnecessary) assignment to a, b, c and d
            a = chrAry[0];
            b = chrAry[1];
            c = chrAry[2];
            d = chrAry[3];
    
            secondBoolean = ((a == '1' || a == '2' || a == '3' || a == '4' || a == '5' || a == '6' || a == '7')
                    && (b == '1' || b == '2' || b == '3' || b == '4' || b == '5' || b == '6' || b == '7')
                    && (c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7')
                    && (d == '1' || d == '2' || d == '3' || d == '4' || d == '5' || d == '6' || d == '7'));
    
            while (!secondBoolean) {
                out.println("The code you typed is not valid. Please type a different code:");
                // ... read in code again. You possibly want to do this with a method
            }
            if (secondBoolean) {
                out.println('0');
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      secondBoolean 变量的值每次都必须重新计算。

      替换

      secondBoolean = ((a == '1'|| a == '2'|| a == '3' || a == '4' || a == '5' || a == '6' || a == '7')
              && (b == '1' || b == '2'|| b == '3' || b == '4' || b == '5' || b == '6' || b == '7')
              && (c == '1' || c == '2'|| c == '3' || c == '4' || c == '5' || c == '6' || c == '7')
              && (d == '1' || d == '2'|| d == '3' || d == '4' || d == '5' || d == '6' || d == '7'));
      
      while (secondBoolean == false) {
          out.println("The code you typed is not valid. Please type a different code:");
          a = myScanner.findWithinHorizon(".", 0).charAt(0);
          b = myScanner.findWithinHorizon(".", 0).charAt(0);
          c = myScanner.findWithinHorizon(".", 0).charAt(0);
          d = myScanner.findWithinHorizon(".", 0).charAt(0);  
          out.print(a);out.print(b);out.print(c);out.print(d);
          } 
          if (secondBoolean == true){
              out.println('0');
          }
      }
      

      while (secondBoolean == false) {
          out.println("The code you typed is not valid. Please type a different code:");
          a = myScanner.findWithinHorizon(".", 0).charAt(0);
          b = myScanner.findWithinHorizon(".", 0).charAt(0);
          c = myScanner.findWithinHorizon(".", 0).charAt(0);
          d = myScanner.findWithinHorizon(".", 0).charAt(0); 
      
          out.print(a);out.print(b);out.print(c);out.print(d);
      
          secondBoolean = ((a == '1'|| a == '2'|| a == '3' || a == '4' || a == '5' || a == '6' || a == '7')
              && (b == '1' || b == '2'|| b == '3' || b == '4' || b == '5' || b == '6' || b == '7')
              && (c == '1' || c == '2'|| c == '3' || c == '4' || c == '5' || c == '6' || c == '7')
              && (d == '1' || d == '2'|| d == '3' || d == '4' || d == '5' || d == '6' || d == '7'));
          } 
          if (secondBoolean == true){
              out.println('0');
          }
      }
      

      您应该在循环的每次迭代中更新secondBoolean 的值。


      此外,您可以通过替换代码来提高代码的可读性

      String choices = "1234567";
      secondBoolean = choices.contains(a + "") && choices.contains(b + "")
                   && choices.contains(c + "") && choices.contains(d + "");
      

      祝你好运

      【讨论】:

        【解决方案4】:

        您处于无限循环中,因为您从未更改secondBoolean 的值。你应该在你的while循环中这样做。

        另外,不要忘记在循环之前初始化你的 secondBoolean 变量(设置 secondBoolean = false,这样你的循环将至少运行一次)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-10-18
          • 2014-03-29
          • 1970-01-01
          • 2013-02-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-01
          相关资源
          最近更新 更多