Java 我的程序没有';t检查else if语句
我对编码还是新手,所以请容忍我。我的程序总是检查if语句中发生了什么,即使值为false,也不转到elseif语句。那么,为什么会发生这种情况呢?任何帮助都将不胜感激Java 我的程序没有';t检查else if语句,java,Java,我对编码还是新手,所以请容忍我。我的程序总是检查if语句中发生了什么,即使值为false,也不转到elseif语句。那么,为什么会发生这种情况呢?任何帮助都将不胜感激 for (int i = 0; i <= time; i++) { System.out.println("Clock loop = " + i); for (int j = 0; j < cashierArr.length; j++) { System.out.println
for (int i = 0; i <= time; i++) { System.out.println("Clock loop = " + i);
for (int j = 0; j < cashierArr.length; j++) {
System.out.println("CashierArr at " + j + " is busy? "
+ cashierArr[j].busy);
System.out.println(j);
if (cashierArr[j].busy = true) {
cashierArr[j].serveTime -= 1;
System.out.println("Serve time = "
+ cashierArr[j].serveTime);
System.out.println("In busy");
} else if (cashierArr[j].busy = false) {
Customer tempCust = (Customer) queue.poll();
cashierArr[j].serveTime = tempCust.serviceTime;
System.out
.println("serveTime = " + cashierArr[j].serveTime);
cashierArr[j].busy = true;
System.out.println("In not busy");
}
}
对于(int i=0;i请执行以下操作
if (cashierArr[j].busy == true) { // == is comparison operator, while = is assign
您只使用了一个将实际分配
值为true意味着它始终保持true
这是一个基本的语法问题
它将值true和false赋给出纳员arr[j]。忙碌
if(cashierArr[j].busy=true)
toif(cashierArr[j].busy==true)
及
else if(cashierArr[j].busy=false)
到else if(cashierArr[j].busy==false
)
假设您的代码可能使用Boolean for busy字段,并且您不希望在null的情况下执行任何操作。执行此操作时:
if (cashierArr[j].busy = true) {
您正在将变量赋值为true,而不检查其中的值
由于您正在检查布尔值,因此足以写入:
if (cashierArr[j].busy) {
在上面的语句中,您使用的是(=)
赋值运算符而不是(=)
比较运算符
=运算符
用于赋值而不是比较,因此实际上是赋值
值设置为true
意味着它始终是resulttrue
,因此您的else语句永远不会被执行
change==在这两个语句中
if(cashierArr[j].busy=true)应该是if(cashierArr[j].busy)并且for(cashierArr[j].busy=false)尝试(!cashierArr[j].busy)您需要在if和if-else条件中添加“=”(双精度)而不是“=”(单精度)。其次,如果busy是布尔类型,则无需检查相等性。或者只需if(cashierArr[j].busy)
if (cashierArr[j].busy) {
1) if(cashierArr[j].busy = true)
2) else if (cashierArr[j].busy = false)
1) if(cashierArr[j].busy == true)
2) else if (cashierArr[j].busy == false)