可以有人解释一下这段代码中的布尔值是如何起作用的吗?

huangapple 未分类评论48阅读模式
英文:

Could someone please explain how the boolean in this code works?

问题

这个问题更多是为了理解我正在处理的一部分代码。第1行中的“valid”布尔值成为第2行while语句的条件。当你将一个布尔值放在while条件中时,这会自动改变布尔值吗?我不明白如果valid被初始化为false,除非在while循环之外它如何变为true - 除非有关它在while条件中的某些特性?我在这里包含了一部分代码,这样你就可以看到发生了什么。这是我正在努力理解的分数计算器的一部分。

感谢你的帮助!

boolean valid = false;
while (!valid) {
    System.out.println("Please enter a Fraction a/b or integer (a): ");
    firstFraction = scan.nextLine();
    valid = validFraction(firstFraction);
    if (valid) {
        int findSlash = firstFraction.indexOf("/");
        if (findSlash >= 0) {
            firstFractionNumerator = Integer.parseInt(firstFraction.substring(0, findSlash));
            firstFractionDenominator = Integer.parseInt(firstFraction.substring(findSlash + 1, firstFraction.length()));
        } 
        else {
            firstFractionNumerator = Integer.parseInt(firstFraction.substring(0, firstFraction.length()));
            firstFractionDenominator = 1;
        }
    }
}
英文:

This question is more to understand a portion of code I am working with. The "valid" boolean in line 1 becomes a condition for the while statement in line 2. When you put a boolean in a while condition, does that automatically change the boolean? I don't understand how if valid is initialized to false, it would ever become true outside of the while loop -unless there is something about it being in the while condition? I'm including a portion of the code so you can see what is happening. This is part of a fraction calculator I'm trying to understand.

Thanks for your help!

boolean valid = false;   
while (!valid) {
    System.out.println("Please enter a Fraction a/b or integer (a): ");
    firstFraction = scan.nextLine();
    valid = validFraction(firstFraction);
    if (valid) {
        int findSlash = firstFraction.indexOf("/");
        if (findSlash >= 0) {
            firstFractionNumerator = Integer.parseInt(firstFraction.substring(0, findSlash));
            firstFractionDenominator = Integer.parseInt(firstFraction.substring(findSlash + 1, firstFraction.length()));
        } 
        else {
            firstFractionNumerator = Integer.parseInt(firstFraction.substring(0, firstFraction.length()));
            firstFractionDenominator = 1;
        }
    }
}

答案1

得分: 0

只在概念上考虑一下。目标是获得一个有效的分数。在询问用户之前,你没有任何分数,所以它是无效的。然后你询问用户,它可以是有效的也可以是无效的。如果它是有效的,你可以退出循环,因为你已经成功了;如果它是无效的,你需要再次询问,所以你需要再次循环。

考虑这个简单的例子。如果我们在6到10之间生成一个随机数,我们将继续循环,如果我们选择了0到5之间的数字,我们将退出。

boolean shouldContinue = true;

while(shouldContinue) {
    shouldContinue = new Random().nextInt(10) > 5;
}
英文:

Just think about it conceptually. The goal is to get a valid fraction. Before you've asked the user, you don't have any fraction, so it's not valid. Then you ask the user and it could be either valid or invalid. If it is valid, you can quit the loop because you've succeeded, if it is invalid, you need to ask them again so you need to loop again.

Consider this simple example. We will continue the loop if we make a random number between 6-10, and quit if we pick a number 0-5

boolean shouldContinue = true;

while(shouldContinue) {
    shouldContinue = new Random().nextInt(10) > 5;
}

答案2

得分: 0

根据代码,它在 while 循环之外并不需要是真实的。循环的概念是在找到有效的 firstFraction 之前持续工作。换句话说,代码会不断要求输入,直到用户提供有效的输入(根据 validFraction 方法中的代码规则判断为有效)。

然后它会获取有效的输入并对其进行处理,if (valid) {} 中的内容与循环的决策无关,仅用于检查输入是 a/b 形式还是整数形式。
然后循环退出,代码继续执行循环之后的实现。

总之,这个循环是有效的。

英文:

As per the code, it is not meant to be true outside the while loop. The concept of the loop is to work until it finds a valid firstFraction. In other words; code will keep asking for input until user gives valid one (valid per code rules in validFraction method).

Then it takes that valid input and processes it, what's in the if (valid) {} is irrelevant to the loop decision making and it's just for checking weather the input is a/b or an integer.
Then loop quits and code proceeds to what's implemented after the while loop.

In conclusion, the loop is good.

huangapple
  • 本文由 发表于 2020年5月29日 08:27:16
  • 转载请务必保留本文链接:https://java.coder-hub.com/62076699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定