public class Game1 {
public Game1 gamet1;
public int card1Hp = 100;
public int card2Hp = 80;
public int card1Apc = 20;
public int card2Apc = 30;
//Constructor
public void start() {
while (card1Hp > 0 ) {
battle();
}
}
public void battle() {
System.out.println(card1Hp - card2Apc);
}
public static void main(String[] args) {
Game1 gamet1 = new Game1();
gamet1.start();
}
}
所以我想在java中做一个while循环,其中war方法在hp> 0时启动但是它不断重复,hp只到70并且不会继续下降到0.我怎么能阻止这个循环时的infinte。它只是继续打印70并且不会停止。
答案 0 :(得分:2)
尝试减少card1Hp。 println仅计算差异并将其打印出来。它不会降低card1Hp。所以:
card1Hp = card1Hp - card2Apc;
System.out.printLn(card1Hp);