Qwisatz
Qwisatz

Reputation: 77

How to repeat while loop for turn base combat java?

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

}

So I want to do a while loop in java where battle method gets launched while hp>0 but it keeps repeating and the hp only goes to 70 and won't keep going down to 0. How can I stop this infinte while loop. It just keeps printing 70 on and on and won't stop.

Upvotes: 1

Views: 205

Answers (1)

Johan Witters
Johan Witters

Reputation: 1636

Try decreasing the card1Hp. The println only calculates a difference and prints it out . It doesn't decrease the card1Hp. So:

 card1Hp = card1Hp - card2Apc;
 System.out.printLn(card1Hp);

Upvotes: 2

Related Questions