Whenever I run the code it just ends regardless of what coinFlip is.ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ
public void fight(String person, int theirHP){
int yourHP = ThreadLocalRandom.current().nextInt(5, 30 + 1);
System.out.println("Your HP: " + yourHP + "\nTheirHP: " + theirHP);
int coinFlip = ThreadLocalRandom.current().nextInt(1, 2 + 1);
int damage;
System.out.println(coinFlip);
if (coinFlip == 1) {
while (yourHP <= 0 || theirHP <= 0) {
damage = ThreadLocalRandom.current().nextInt(1, 3 + 1);
theirHP -= damage;
System.out.println("Their HP: " + theirHP + "\nYour HP: " + yourHP);
damage = ThreadLocalRandom.current().nextInt(1, 3 + 1);
yourHP -= damage;
System.out.println("Their HP: " + theirHP + "\nYour HP: " + yourHP);
}
}
if (coinFlip == 0) {
while (yourHP <= 0 || theirHP <= 0) {
damage = ThreadLocalRandom.current().nextInt(1, 3 + 1);
yourHP -= damage;
System.out.println("Their HP: " + theirHP + "\nYour HP: " + yourHP);
damage = ThreadLocalRandom.current().nextInt(1, 3 + 1);
theirHP -= damage;
System.out.println("Their HP: " + theirHP + "\nYour HP: " + yourHP);
}
}
}
ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ
>Solution :
Your while loops will only be entered when the condition evaluates as true. I would assume both yourHP and theirHP start with values greater than 0 so
while (yourHP <= 0 || theirHP <= 0)
evaluates as false. I believe you wanted
while (yourHP > 0 && theirHP > 0)
To end when either values decrease to 0.
Also, and not directly to your issue,
if (coinFlip == 1)
should logically be followed by an else. Not another test like if (coinFlip == 0). But both bodies appear to do the same thing, so it isn’t clear why you have coinFlip in the first place.