即使条件不满足,如果语句始终在运行,也很奇怪 [英] Strange if statement always running even if conditions are not met

查看:67
本文介绍了即使条件不满足,如果语句始终在运行,也很奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对if语句有一个奇怪的问题.我正在检查布尔值是否为true,是否必须运行if语句.我在很多地方都使用System.out.println布尔值,其值始终为false,但if语句仍在运行.有帮助吗?

I have a strange problem with an if statement. I'm checking if a Boolean is true, if it is the if statement must run. I System.out.println the value of the boolean at a lot of places, its always false but the if statement still runs. Any help ?

    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;
}

}

推荐答案

在该行代码中有一个小的语法错误.

You have a small syntax error in that line of code.

代替

if (checkWin());

应该是

if (checkWin())

删除;

这篇关于即使条件不满足,如果语句始终在运行,也很奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆