gpt4 book ai didi

java - (非常基础)Java在while循环中设置 boolean 值

转载 作者:行者123 更新时间:2023-12-01 18:15:49 25 4
gpt4 key购买 nike

主要:

public class ElevatorTest {
public static void main (String []args) {
Elevator.currentLevel(); // output current level
Elevator.go(2); // move elevator up 2 floors
Elevator.currentLevel();
}
}

等级电梯:

public class Elevator {
public static int currentLevel = 0;
public static boolean validChoice;

public static void currentLevel() {
System.out.println(currentLevel);
}

public static void go(int arg) {
while (validChoice = false) {
// this building has twenty floors and a basement.
// Ground floor is 0 and there are 20 floors above it
if (-1 < currentLevel + arg && currentLevel + arg < 20) {
currentLevel = currentLevel + arg;
validChoice = true;
} else {
System.out.println("You tried to go too high or too
low. Try again.");
validChoice = false;
}
}
}
}

第十行课Elevator ,IntelliJ 告诉我 boolean 值 validChoice总是假的。如何让程序仅在满足 else 语句时才将此 boolean 值设置为 false?

最佳答案

对于 while 循环 while (validChoice = false) { 应该有 2 个等号 == 来指示比较。

validChoice 始终为 false 的原因是因为您始终将 validChoice 指定为 false。

关于java - (非常基础)Java在while循环中设置 boolean 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29639353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com