2016-07-11 2 views
-2

텍스트 어드벤처 게임을 만들고 프로그램이 종료되었습니다. 왜 누군가가 도움을 줄 수 있는지 확인하지 못합니까? 정말 바보 같은 실수를해서 미안해, 나는 초보자이다. 감사합니다 :)Java; Statement로 종료 된 코드

//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()'이라는 줄로 한 번만 사용자 입력을 요구합니다. 루프를 반복하여 사용자에게 메시지를 표시해야합니다. 당신이 자바 프로그래밍에 익숙한가요? 그렇다면 게임을 작성하기 전에 자바와 프로그래밍에 익숙해지기를 원할 것입니다. –

+1

어딘가에'userInput = userInput.toLowerCase();'라고 써서 모든 곳의 'equalsIgnoreCase' 대신'equals'를 사용하면됩니다. –

답변

0

문제는 단지 그것을 변경하는 기대 다음 번 입력을 받고있는 것입니다.

예를 들어 사용자가 "blue door"에 대해 뭔가를 입력했는지 확인한 다음 동일한 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 
} 
관련 문제