-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");
}
您只提示用戶輸入一次,用'String userInput = myScanner.nextLine()'行。您需要循環提示用戶一遍又一遍。您是否熟悉用java編程,如果不熟悉,那麼在嘗試編寫遊戲之前,您可能希望更熟悉java和編程(算法等)。 –
你應該說'userInput = userInput.toLowerCase();'在某處,這樣你就可以簡單地在每個地方使用'equals'而不是'equalsIgnoreCase'。 –