if (sum < 6) {
System.out.println("You win");
System.out.println();
// Does the user want to retry?
System.out.print("Would you like to retry?(Y or N) : ");
String retry = input.nextLine();
while (true) {
// If they say y or Y, roll again
if (("y".equals(retry)) || ("Y".equals(retry))) {
roll();
// Check for anything other than y and Y
} else if (("n".equals(retry)) || ("N".equals(retry))) {
System.out.println("Closing");
break;
} else if (!("y".equals(retry)) || !("Y".equals(retry))) {
System.out.print("Invalid input. Would you like to retry?(Y or N) : ");
retry = input.nextLine();
System.out.println();
}
}
} else if (sum > 6) {
System.out.println("You lose");
System.out.println();
System.out.print("Would you like to retry?(Y or N) : ");
String retry = input.nextLine();
while (true) {
if (("y".equals(retry)) || ("Y".equals(retry))) {
roll();
} else if (("n".equals(retry)) || ("N".equals(retry))) {
System.out.println("Closing");
break;
} else if (!("y".equals(retry)) || !("Y".equals(retry))) {
System.out.print("Invalid input. Would you like to retry?(Y or N) : ");
retry = input.nextLine();
System.out.println();
}
}
}
I'm trying to make a dice game where the game will keep rolling the dices when the user inputs "y"
or "Y"
. I also want it to stop the game and say "Closing"
when the user inputs "n"
or "N"
.
The issue is, when the user inputs "n"
or "N"
, it will print out "Closing"
but the loop doesn't stop and the game will roll the dices again. How to I make my while() loop stop when the user inputs "n"
or "N"
?
Here's the output when the user chooses to stop the game ("n" or "N")
: Would
you like to retry?(Y or N) : n
Closing
Rolling...
You rolled : 1 & 4
Sum = 5
You win
I'm sorry for such basic question, I am new to programming.