[英]While loop doesn't change the value of variable
int time=0;
int x=1;
int w=2;
int y=1;
int h=1;
int t=6;
while (x != w && y != h) {
boolean s = Abc (a,b,c,time);
if (s == true) {
time = time+t;
x++;
}
else if (s = false) {
time = time++;
}
}
System.out.println(time);
This is a part of program I'm writing for school and my problem is that when the program enters the while loop no matter what I do inside the loop when it exits and prints the variable "time" the value is the same as the value set at the beginning. 这是我正在为学校编写的程序的一部分,我的问题是,当程序进入while循环时,无论我在退出并打印变量“ time”时在循环中执行什么操作,该值都与该值相同设置在开始。 Am I missing something?
我想念什么吗? (BTW Abc is just a method that should return the value true when I first enter the loop, therefore the value of time should be changed to time+t)
(BTW Abc只是一种方法,当我第一次进入循环时应返回true值,因此time的值应更改为time + t)
s = false
is an assignment, not a comparison. s = false
是分配,而不是比较。 Anyway, you don't need to test if s == false
, you just need an else
clause (since the original condition checks for s == true
). s == false
,只需要一个else
子句(因为原始条件检查s == true
)。 time++
to time
, since it will undo the increment (the post increment operator returns the previous value of the variable, so when you assign that value back to the variable, you cancel the increment). time++
的结果分配给time
,因为它会撤消增量(后增量运算符将返回变量的前一个值,因此,当将该值分配回变量时,您将取消增量)。 Change 更改
else if (s = false) {
time = time++;
}
to 至
else {
time++;
}
Edit : 编辑:
As Anthony Grist commented, even after these fixes, the entire while loop won't be executed at all, since y==h
in the beginning. 正如Anthony Grist所评论的那样,即使在修复了这些问题之后,也完全不会执行整个while循环,因为开头的
y==h
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.