gpt4 book ai didi

java - 奇怪的 if 语句即使不满足条件也总是运行

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

我的 if 语句有一个奇怪的问题。我正在检查 boolean 值是否为真,如果是则必须运行 if 语句。我在 System.out.println 的很多地方都使用了 boolean 值,它总是 false 但 if 语句仍然运行。有什么帮助吗?

    import java.util.ArrayList;

public class GameLoop extends Thread {
private Boolean running;
private ArrayList<Meteor> meteorArrayList;
private Boolean win;
private GameScreen gameScreen;

public GameLoop(GameScreen gameScreen, ArrayList<Meteor> meteorArrayList)
{
setMeteorArrayList(meteorArrayList);
setGameScreen(gameScreen);
setRunning(true);
setWin(false);
}

@Override
public void run()
{
while (running)
{
if(checkWin());
{
System.out.println(checkWin());
gameScreen.winGame();
}
}
}

public Boolean checkWin()
{
if(getMeteorArrayList().isEmpty())
{
setWin(true);
}
else
{
setWin(false);
}
System.out.println(getWin());
return getWin();
}

public void cancel()
{
interrupt();
}

//======================GETTER EN SETTERS=============================


public Boolean getRunning() {
return running;
}

public void setRunning(Boolean running) {
this.running = running;
}

public ArrayList<Meteor> getMeteorArrayList() {
return meteorArrayList;
}

public void setMeteorArrayList(ArrayList<Meteor> meteorArrayList) {
this.meteorArrayList = meteorArrayList;
}

public Boolean getWin() {
return win;
}

public void setWin(Boolean win) {
this.win = win;
}

public GameScreen getGameScreen() {
return gameScreen;
}

public void setGameScreen(GameScreen gameScreen) {
this.gameScreen = gameScreen;
}

}

最佳答案

该行代码中有一个小语法错误。

而不是

if (checkWin());

应该是

if (checkWin())

删除;

关于java - 奇怪的 if 语句即使不满足条件也总是运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59098361/

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