I am trying to use a loop and if/else statements along with my Switch to create a very simple type adventure.
I want there to be a boolean to add my quit function, but am wondering if a Sentinel-controlled loop would be better. I simply need the player to be able to travel from room to room, an error message come up if they press the wrong button, allowing them to try again, and then a quit function available at all times. I know how to add a quit function to each input section but would have been trying to use a boolean and while loop to solve this, to no avail.
Here is my current code (not finished but my only trouble thus far is with the quit function)
it compiles and runs but only follows through with the quit function in the first step and when it reaches case 2.(Dinner Hall) I cannot get it to enter into the next room.
Thanks:
import java.util.Scanner;
public class Boolean2
{
public static void main(String[] args)
{
int room;
String command=null;
String command2=null;
boolean quit;
room=1;
System.out.println("Press s to start!");
{Scanner keyboard= new Scanner(System.in);
command=keyboard.nextLine();
if (command.equals("q"))
{quit=true;
System.out.println("Game Over");
System.exit(0);}
else
{System.out.println("Let's Begin The Adventure!!");}
quit=false;
while (quit==false)
{
switch(room){
case 1:
System.out.println("You are in the Kitchen,you may go N");
break;
case 2:
System.out.println("You are in the Dinner Hall,you may go S or E");
break;
case 3:
System.out.println("You are in the Master Bedroom,you may go E or W");
break;
case 4:
System.out.println("You are now a victor!!");
System.out.println("Game Over");
quit=true;
break;
default:
System.out.println("A error has occured");
quit=true;
break;
}
if (quit==false)
command=keyboard.nextLine();
if (command.equals("N"))
{room=2;
System.out.println("Welcome to room 2");}
else
{System.out.println("Error!!!");}
}
command2=keyboard.nextLine();
if (command2.equals("E"))
{room=3;
System.out.println("Welcome to room 3");}
else
{System.out.println("Error!!");}
}
}
}