Java; Code Terminated with If Statments

时间:2016-07-11 20:20:40

标签: java

I am making a text adventure game and the program terminated and I cant identify why can someone help? Sorry probably making really dumb mistake, I'm a novice. Thanks :)

//Set up scanner "userInput"
System.out.println("You are in a room");
myScanner = new Scanner(System.in);
String userInput = myScanner.nextLine();

//Variables
int bone = 0;
int flashlight = 0;

//Begin Adventure

if (userInput.equalsIgnoreCase("look")
    || userInput.equalsIgnoreCase("look around")
    || userInput.equalsIgnoreCase("obsererve surroundings")) {
  System.out.println(
      "There are 4 doors; one is blue, one is green, one is red, and one is yellow");
} else {
  System.out.println("You cant do that");
}
if (userInput.equalsIgnoreCase("enter blue door")
    || userInput.equalsIgnoreCase("enter blue room")
    || userInput.equalsIgnoreCase("go into blue door")
    || userInput.equalsIgnoreCase("go into blue room")) {
  System.out.println("The room is pitch black");
  if (flashlight == 1) {
    if (userInput.equalsIgnoreCase("use flashlight")
        || userInput.equalsIgnoreCase("use flash light")) ;
    System.out.println("Light! You can see a bone on a table.");
    if (userInput.equalsIgnoreCase("take bone") || userInput.equalsIgnoreCase("grab bone")) ;
  }
  if (userInput.equalsIgnoreCase("leave room")
      || userInput.equalsIgnoreCase("exit room")
      || userInput.equalsIgnoreCase("exit")) ;
  System.out.println("You return to the central room.");
}
if (userInput.equalsIgnoreCase("enter red door")
    || userInput.equalsIgnoreCase("go through red door")
    || userInput.equalsIgnoreCase("enter red room")
    || userInput.equalsIgnoreCase("enter red room")) {
  System.out.println("There is a man sitting in the chair");
}
if (userInput.equalsIgnoreCase("talk to man") || userInput.equalsIgnoreCase("talk")) {
  System.out.println("He tells you that you need to go to the yellow room");
}
if (userInput.equalsIgnoreCase("leave room")
    || userInput.equalsIgnoreCase("exit room")
    || userInput.equalsIgnoreCase("exit")) {
  System.out.println("You return to the central room");
}
if (userInput.equalsIgnoreCase("enter yellow door")
    || userInput.equalsIgnoreCase("enter yellow room")
    || userInput.equalsIgnoreCase("go into yellow door")
    || userInput.equalsIgnoreCase("go into yellow room")) {
  System.out.println("There is a flashlight on a table");
}
if (userInput.equalsIgnoreCase("take flashlight")
    || userInput.equalsIgnoreCase("grab flashlight")
    || userInput.equalsIgnoreCase("take flash light")
    || userInput.equalsIgnoreCase("grab flash light")) {
  System.out.println("You got that flashlight man");
}

1 个答案:

答案 0 :(得分:0)

Your issue is that you're only getting input once and then expecting it to change.

For example, you check if the user entered something about "blue door", then check if the same userInput value now contains "flashlight." That will never be true.

You need to loop over input, and make your branching logic save state and vary based on current location. There are many ways to do this, but you probably need to store location, a list of inventory items, and any other state that you might rely on. For example, if the user goes in the red door and looks for a flashlight, they won't find one. If they go in the blue door and look for the bone before finding the flashlight, they won't find anything. The logic to do this is beyond the scope of your problem, but a simple example to get you started is below.

System.out.println("You are in a room");
myScanner = new Scanner(System.in);
while(myScanner.hasNext()) {
    String userInput = myScanner.nextLine();    
    processInput(userInput);
}

...

void processInput(String input) {
    // process input, while maintaining state of where the user currently is
}