0

I am trying to make my code re-print a line if the value isn't met.

I have tried using while but it wont revert back to the question if 'X' isn't greater than or equal to 1.

I am currently trying:

    import java.util.Scanner;
public class rpg {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double hp = 10;
        System.out.println("how much damage do you wish to do?");
        double attack = input.nextDouble();
        double damage = hp - attack;
        System.out.println(damage);
            System.out.println("health = " + Math.round(hp));
            while (hp <= 1) {
                System.out.println("Alive");
                break;
                }
            }
    }

but I can't get the question to re state when the hp is greater than 1 still.

jmarkmurphy
  • 11,030
  • 31
  • 59

1 Answers1

0

This should work read the comments

public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    double hp = 10;
    while(hp > 1) { //Moved The loop
        System.out.println("how much damage do you wish to do?");
        double attack = input.nextDouble();
        //double damage = hp - attack;//not needed
        hp = hp - attack;//Added this line
        //System.out.println(damage);//not needed
        System.out.println("health = " + Math.round(hp));
        if(hp == 0)
            System.out.println("Dead");
        else
            System.out.println("Alive");
    }
}
Matt
  • 3,052
  • 1
  • 17
  • 30
  • Instead of asking a whole new question I thought i would ask you here. Are you able to help, if hp = 0 i want the statement to print out "dead" because the console adds the "Alive" even when hp = 0 – Alex O'Malley Sep 19 '18 at 16:22
  • @AlexO'Malley like that? – Matt Sep 19 '18 at 16:26