无限循环涉及Java中的布尔变量

时间:2014-04-04 23:04:32

标签: java while-loop boolean infinite-loop

这是我的第一篇文章,如果我未能在本网站上遵循正确的格式/惯例等,请致以道歉。我只编程了几个星期。

无论如何,我正在使用布尔变量和while循环编写基本的Java程序。代码非常“笨拙”。至少可以说,并且绝对可以做得更优雅(虽然不是我的技能有限,所以再次道歉)。 如果布尔变量为true,则代码可以正常工作。但是,如果变量为false,则代码转到while循环,即使放入了一个输入应该使该语句为true,循环也会继续。任何想法是什么导致这种无限环。我几乎可以肯定它是基本的东西,但我似乎无法弄明白。这是下面的代码。谢谢!

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

4 个答案:

答案 0 :(得分:1)

每次都必须重新计算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 + "");
祝你好运

答案 1 :(得分:1)

您处于无限循环中,因为您永远不会更改secondBoolean的值。你应该在你的while循环中使用它。

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

答案 2 :(得分:0)

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

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 :(得分:0)

是的,当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,但这看起来有点整洁! 希望有所帮助。