簡體   English   中英

執行時發出while循環問題

[英]Issue with a while-loop while executing it

我有一個用swing實現小游戲的程序,在主類中我有一個循環,它會在游戲地圖中監聽一個布爾值。 與實現的循環while如果它是唯一一個不執行的指令,我不`噸知道為什么。

我嘗試使用其他一些指令來檢查循環是否繼續,並且它工作正常,但是當我只放置if我想要它不起作用。

public class ejecutor extends JFrame{
boolean ejecutando = true;

    public ejecutor(){
        mapa mapa = new mapa(); //the map of my game
        setSize(665,690);
        add(mapa);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
        while(ejecutando){ 
            if(mapa.todasComidas()){
            //wLabel();
            ejecutando=false;
            System.out.println("You finished!!");
            }
//If I put this println it works, if I only use the If, without any other instruction inside the while, it does not do anything at all
            System.out.println("............");
        }
    }
//I execute the constructor in the main...

默認情況下,Java中Java對象的布爾變量設置為默認值為false 查看代碼mapa.todasComidas() ,您從未明確設置相同。

現在在你的while循環中,你正在檢查todasComidas方法是否返回true ,而不是這種情況,因此永遠不會重置相同的,即執行if條件的內部語句。 您的外部聲明會被執行並在控制台上打印,因為它沒有被任何條件保護。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM