为什么我的循环不结束?

Why won't my loop end?

在我制作的游戏中,我有一个循环在 libgdx 屏幕的显示方法中运行。循环基本上表示战斗正在进行。这是一个 while 循环,在布尔值 "iscombat = true" 时运行。但是,在循环中我为 iscombat 设置了一个条件为 = false,这似乎并不重要。循环继续进行。我想也许我犯了某种错误所以我输入了一个递增的整数并告诉循环在该整数达到 30 时结束。循环继续进行。

然后我写信让它打印出整数的值,以防它不递增。不,我看着它超过 30,循环仍然没有结束。所以我写了一个程序,当整数超过 30 时,它会直接改变屏幕,它仍然保持 运行 循环,即使它打印出整数超过 30。

这是 while 循环的代码:

while(combatisgoing = true){                                         //begin the combat loop.  all of combat happens in this loop?
blah++;    
System.out.println(blah);
thisencounter.TurnCounter();                                        //we are in combat now, so start the turn counting 

if(blah > 30){((Game)Gdx.app.getApplicationListener()).setScreen(new Home());}
if(blah > 30){  combatisgoing = false; }

if(thisencounter.monster1turn=true){                                //monster1's turn goes in here
    thiscombat.getTarget(1);                                        //get enemy's target
    thiscombat.calculateVars(1);                                     //calculate all combat variables for this turn
    thiscombat.doAttack();
    System.out.println(Encounter.enemies.get(0).species+"attacked");
    System.out.println("Your spirits healths are:"+OwnedSpirits.myspirits.get(0).health+""+OwnedSpirits.myspirits.get(1).health+OwnedSpirits.myspirits.get(2).health);
thisencounter.monster1turn=false;
}    

}

还有比这更多的代码,但其他内容根本不相关。 如果你想看看 turncounter 是什么样子,我不明白它为什么重要,但它就在这里

 public void TurnCounter(){
    x=0;
    while(x==0){
    monster1rate = monster1rate + monster1rate2;
    monster2rate = monster2rate + monster2rate2;
    monster3rate = monster3rate + monster3rate2;
    spirit1rate = spirit1rate + spirit1rate2;
    spirit2rate = spirit2rate + spirit2rate2;
    spirit3rate = spirit3rate + spirit3rate2;

if(monster1rate > 100000){

  monster1rate = monster1rate - 100000;
  monster1turn = true;
  x=1;
}
else if(monster2rate > 100000){

  monster1rate = monster1rate - 100000;
  monster2turn = true;
  x=1;
}
else if(monster3rate > 100000){

  monster1rate = monster1rate - 100000;
  monster3turn = true;
  x=1;
}
else if(spirit1rate > 100000){

  spirit1rate = spirit1rate - 100000;
  spirit1turn = true;
  x=1;
}
if(spirit2rate > 100000){

  spirit2rate = spirit2rate - 100000;
  spirit2turn = true;
  x=1;
}
if(spirit3rate > 100000){

  spirit3rate = spirit3rate - 100000;
  spirit3turn = true;
  x=1;
}



}
}

这只是确定我的游戏轮到谁的一种方式。

游戏的输出刚刚开始,它一直递增,当我进站时它看起来像这样:

"Red Sadsackattacked
Your spirits healths are:151715
your spirit3 would go now
793
Red Sadsackattacked
Your spirits healths are:151715
your spirit3 would go now
794
Red Sadsackattacked
Your spirits healths are:151715
your spirit3 would go now
795
Red Sadsackattacked
Your spirits healths are:151715
your spirit3 would go now
796
Red Sadsackattacked
Your spirits healths are:151715
your spirit3 would go now
797
Red Sadsackattacked
Your spirits healths are:151715
your spirit3 would go now
798"

这正是我期望我的输出看起来的样子,除了当 blah > 30 时循环不会停止。

请帮忙哈哈

这是一个始终true

的作业
while (combatisgoing = true) {      

改为

while (combatisgoing) {      

如果while(combatisgoing) 让你感到困惑... 尝试 while(combatisgoing==true)

一个等于表示您正在设置一个值。在这种情况下,您正在使 combatisgoing 始终为真。两个等于是判断某物是否等价的运算符。