Can I repeat a section of code until I get a desired answer
so I've been learning java in my CS class at my high school, and I decided to remake a game that was my final project last year and add a bit more to it.
My problem is that I have the player input a number in order to make a specific choice, but if they input an invalid option, the code just ends, I've found a user with a similar problem here, but they are using if/else statements, and I'm using a switch statement
//shows the options the player can pick
System.out.println("Start Game: 1");
System.out.println("Close Game: 2");
String choice = input.nextLine();
switch(choice)
{
case "1": Variables.clear(); introduction(); break;
case "2": System.exit(0);
default: Variables.invalidAnswer();
}
public static void introduction()
{
System.out.println("test");
}
One of the answers was to try using a while(true) loop, which I tried
while(true)
{
//shows the options the player can pick
System.out.println("Start Game: 1");
System.out.println("Close Game: 2");
String choice = input.nextLine();
switch(choice)
{
case "1": Variables.clear(); introduction(); break;
case "2": System.exit(0);
default: Variables.invalidAnswer(); Variables.clear();
}
}
but when I do that, it just loops no matter what, I thought about adding another break statement after case "1", but I realized that wouldn't work. So I was wondering if there was a way to be able to make this work, or if there was a solution like this that works with a switch statement, any advice would be great, thanks.
Solution 1:
I'm always curious as to why people seem to forget that a do-while
actually exists. Consider for a moment, the overall requirement - you must perform the loop at least once, so, we don't care about the exit condition until the end of the loop, for example...
String exitOption = "2";
String choice = null;
do {
System.out.println("Start Game: 1");
System.out.println("Close Game: 2");
choice = input.nextLine();
switch (choice) {
case "1":
Variables.clear();
introduction();
break;
default:
Variables.invalidAnswer();
}
} while (!exitOption.equals(choice));
Here, I'm assuming you only ever want to exit the loop when the user selects 2
, just beware of that
Flow control is a pretty basic concept of programming, you should take a closer look up:
- Control Flow Statements
- The while and do-while Statements
Solution 2:
Doing a break
inside a switch-block will break the switch-block, not the while-loop. To break the outer while-loop, you can use a label:
import java.util.Scanner;
class Program {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
inputLoop: while (true) {
System.out.println("Start Game: 1");
System.out.println("Close Game: 2");
String choice = input.nextLine();
switch(choice)
{
case "1": {
// Start the game
System.out.println("Game started");
break inputLoop;
}
case "2": {
// End the game
System.out.println("Game finished");
break inputLoop;
}
default: {
// Do nothing (the loop will be repeated)
System.out.println("Invalid answer");
}
}
}
}
}