英文:
Java boolean in if statements
问题
当我运行这段代码时,我得到了值"no"作为输出。我不明白为什么布尔变量 isNegative 在 x 被初始化为 100 的情况下仍然保持为 true。这是在 Java 中。感谢未来的帮助。
int x = -555;
boolean isNegative = (x < 0);
if (isNegative)
{
x = 100;
if (isNegative)
System.out.println("no");
else
System.out.println("yes");
}
else
System.out.println("maybe");
英文:
When I run this code I have the value no as an output. I don't understand why boolean variable isNegative stays as true while x is initiated as 100. This on java. Thank for the future help.
boolean isNegative = (x < 0);
if (isNegative)
{
x = 100;
if (isNegative)
System.out.println("no");
else
System.out.println("yes");
}
else
System.out.println("maybe");
答案1
得分: 2
因为isNegative
的值从未被改变,只需在更改x
时更新该值。
boolean isNegative = (x < 0);
if (isNegative)
{
x = 100;
isNegative = (x < 0);
if (isNegative)
System.out.println("no");
else
System.out.println("yes");
}
else
System.out.println("maybe");
英文:
because the value of isNegative
is never changed, just update the value when you change x
boolean isNegative = (x < 0);
if (isNegative)
{
x = 100;
isNegative = (x < 0);
if (isNegative)
System.out.println("no");
else
System.out.println("yes");
}
else
System.out.println("maybe");
答案2
得分: 0
因为你的布尔值在 if 语句之前被初始化。
英文:
that because you boolean is initialized before your if statement
答案3
得分: 0
因为 (x < 0) 是在运行时仅评估一次的表达式。这个
boolean isNegative = (x < 0);
被执行一次。之后,isNegative 将始终为 false(假设 x 已经是负数)。如果您稍后更改了 'x',那么需要重新评估或重新计算 isNegative。
英文:
Because (x < 0) is an expression evaluated at runtime just one time. This
boolean isNegative = (x < 0);
gets executed just one time. After that, isNegative will be always false (assuming x is already negative). If you later change 'x', then isNegative needs to be re-evaluated or re-calculated.
答案4
得分: 0
我可能在重复别人说过的话,但或许我可以补充一些。这个问题显示了对程序执行的误解,这是非常基础的。
执行按语句序列进行。这个序列会被循环、方法调用等修改,但这与本答案无关。
赋值(给变量)是一种计算值的语句,可能会使用当前可用的其他值。它绝对不是与方程相同,方程说明两个表达式意思相同。
所以,在你的情况下,
isNegative = (x < 0);
会查看在语句遇到时的x
的值,计算x < 0
的值(可能为true或false),并将该值赋给isNegative
。然后我们继续下一个语句。
如果之后没有对isNegative
的赋值,那么它的值将不会改变。它并没有以某种方式与x
的值连接在一起,使得isNegative
会自动反映x
的当前符号。
附:如果你愿意,你可以定义一个有用的方法:
boolean isNegative(int val) {
return val < 0;
}
然后在想要测试x
的当前值时使用isNegative(x)
。
值得一提的是,编程中“变量”的概念是编程特有的东西,有许多杰出的计算机科学家会说这是一个相当深刻的思想。
英文:
I'm probably repeating what others have said, but perhaps I can add something. The question shows a misunderstanding of program execution, which is pretty fundamental.
Execution proceeds through a sequence of statements. The sequence is modified by loops, method calls, and so on, but that's not really germane to this answer.
An assignment (to a variable) is a statement that computes a value, possibly using other values available right now. It is most definitely not the same thing as an equation, which states that two expressions mean the same thing.
So, in your case,
isNegative = (x < 0);
looks at the value of x
at the time the statement is encountered, computes the value of x < 0
(which is either true or false), and assigns that value to 'isNegative`. Then we proceed to the next statement.
If there is no subsequent assignment to isNegative
then its value will not change. It has not somehow been yoked to the value of x
in a way that isNegative
will automatically reflect the current sign of x
.
Aside: you could, if you wanted, define a useful method:
boolean isNegative(int val) {
return val < 0;
}
and then use isNegative(x)
when you wanted to test the current value of x
.
For what it's worth, the concept of 'variable' in the sense of programming is a thing peculiar to programming, and there are many eminent computer scientists who'll say it's quite a deep idea.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论