2016-07-11 30 views
-2

我正在做一個文本冒險遊戲,程序終止,我不能確定爲什麼有人可以幫忙?對不起,可能真的很愚蠢的錯誤,我是一個新手。 謝謝:)Java;代碼終止,如果陳述

//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

您只提示用戶輸入一次,用'String userInput = myScanner.nextLine()'行。您需要循環提示用戶一遍又一遍。您是否熟悉用java編程,如果不熟悉,那麼在嘗試編寫遊戲之前,您可能希望更熟悉java和編程(算法等)。 –

+1

你應該說'userInput = userInput.toLowerCase();'在某處,這樣你就可以簡單地在每個地方使用'equals'而不是'equalsIgnoreCase'。 –

回答

0

您的問題是,你只得到輸入一次,然後希望它改變。

例如,您檢查用戶是否輸入了有關「藍色門」的內容,然後檢查相同的userInput值是否包含「手電筒」。那永遠不會是真的。

您需要循環輸入,並使分支邏輯保存狀態並根據當前位置而變化。有很多方法可以做到這一點,但您可能需要存儲位置,清單項目清單以及您可能依賴的任何其他狀態。例如,如果用戶進入紅門並尋找手電筒,他們將找不到一個。如果他們在找到手電筒之前進入藍色的門並尋找骨頭,他們將找不到任何東西。執行此操作的邏輯超出了您的問題範圍,但下面是一個簡單的示例。

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 
}