2014-09-14 11 views
0

그래서 텍스트 기반 모험을 만들려고합니다. 방에서 시작하여 두 가지 방 옵션을 제공합니다. 거기에서 각 방 옵션에는 2 개의 추가 옵션이 있습니다. 그리고 각 방에는 "예"또는 "아니오"옵션이 있습니다."변수가 초기화되지 않았을 수 있습니다"- Java 기반 텍스트 기반 어드벤처

총 15 개의 객실과 8 개의 다른 결과가 있습니다. if 문을 사용하여 이것을 생성해야합니다. 그러나 변수 "room2, room3, room4, room5, room6, & room7에 대해"변수가 초기화되지 않았을 수 있습니다. "라는 오류가 계속 나타납니다. 나는 변수가 if 문 안에서 선언 되었기 때문에 그것이 맞다는 것을 알고 있지만 올바른 해결책을 모른다.

도움을 주시면 감사하겠습니다. 감사.

import java.util.Scanner; 
public class Adventure1 
{ 
    public static void main(String[] args) 
    { 
     Scanner keyboard = new Scanner(System.in); 

     String room1, room2, room3, room4, room5, room6, room7; 

     System.out.println("Welcome to Carolyn's Tiny Adventure! "); 
     System.out.println("You are in a SUPER creepy house! "); 
     System.out.println("Do you want to go upstairs or into the basement ? "); 
     System.out.println("(upstairs OR basement)"); 
     room1 = keyboard.next(); 

     if (room1.equals("upstairs")) 
     { 
      System.out.println("You notice there is a ladder that leads to an attic."); 
      System.out.println("Do you want to go up or explore the bedrooms? (up OR bedrooms) "); 
      room2 = keyboard.next(); 
     } 

     if (room1.equals("basement")) 
     { 
     System.out.println("There's nothing in the basement except an old rocking chair & a dusty  cabinet. "); 
     System.out.println("Do you want to open or would you rather rock on the chair? (open OR chair) "); 
     room3 = keyboard.next(); 
    } 

    if (room2.equals("up")) 
    { 
     System.out.println("It's super dark up here, & there's dust and spiders everywhere. "); 
     System.out.println("But you see a cool looking chest. Do you want to open it? (yes OR no) "); 
     room4 = keyboard.next(); 
    } 

    if (room2.equals("bedrooms")) 
    { 
     System.out.println("You find what looks like a child's bedroom. "); 
     System.out.println("Do you want to see what's inside the closet? (yes OR no) "); 
     room5 = keyboard.next(); 
    } 

    if (room3.equals("open")) 
    { 
     System.out.println("Inside the chest is an opaque jar. You can't tell what's inside. "); 
     System.out.println("Do you want to open it? (yes OR no) "); 
     room6 = keyboard.next(); 
    } 

    if (room3.equals("chair")) 
    { 
     System.out.println("You're walking over to sit on the chair & you notice it's a little sideways."); 
     System.out.println("Do you want to sit on it anyway? (yes OR no) "); 
     room7 = keyboard.next(); 
    } 

    if (room4.equals("yes")) 
    { 
     System.out.println("Uh-oh! You've been bit by a black widow."); 
     System.out.println("Thanks for playing with me!!!"); 
    } 

    if (room4.equals("no")) 
    { 
     System.out.println("Welp, I guess you'll never know what was inside!"); 
     System.out.println("Thanks for playing with me!!!"); 
    } 

    if (room5.equals("yes")) 
    { 
     System.out.println("YIKES! There's a creepy doll staring at you."); 
     System.out.println("That image may never leave your mind... "); 
     System.out.println("Thanks for playing with me!!! "); 
    } 

    if (room5.equals("no")) 
    { 
     System.out.println("I guess you'll never know what was inside... "); 
     System.out.println("Thanks for playing with me!!! "); 
    } 

    if (room6.equals("yes")) 
    { 
     System.out.println("EW! There's a rotting bird in there! You just threw up. "); 
     System.out.println("Thanks for playing with me!!! "); 
    } 

    if (room6.equals("no")) 
    { 
     System.out.println("You'll never know what was inside! Your loss! "); 
     System.out.println("Thanks for playing with me!!! "); 
    } 

    if (room7.equals("yes")) 
    { 
     System.out.println("When you sat down it fell apart! Now dust is all over your butt. Whoops."); 
     System.out.println("Thanks for playing with me!!! "); 
    } 

    if (room7.equals("no")) 
    { 
     System.out.println("Smart choice! It looks like that thing was going to fall apart any second."); 
     System.out.println("Thanks for playing with me!!! "); 
    } 
} 

}

+1

단순 : 컴파일러에서 불평하는 변수를 초기화하십시오. * 일부 값, 문자열 일 경우'null' 또는''''중 하나를 지정하십시오. –

+0

개념 상 오류가 발생했습니다 : 사용자가 "위층에"입력하지 않았을 때'if (room2.equals ("up"))'가 쉽게'NullPointerException'을 일으킬 수 있습니다. 사용자가 "지하실"과 다른 것을 입력하면 'room3'도 마찬가지입니다. – Tom

+1

답변을 찾은 경우 질문 제목을 수정하지 마십시오. 대신 투표 카운터 아래의 체크 표시를 클릭하여 해당 답변을 "수락 됨"으로 표시하십시오. – Tom

답변

0
이 줄을 변경

String room1, room2, room3, room4, room5, room6, room7; 

String room1 = "", room2 = "", room3 = "", room4 = "", room5 = "", room6 = "", room7 = ""; 

아니면

할당되지 같은 다른 어떤 관련성 값일 수있다 할당 할 수

room1 = "not assigned" 
2

는 변수를 초기화 :

String room1, room2, room3, room4, room5, room6, room7; 

String room1 = null; 
String room2 = null; 
String room3 = null; 
String room4 = null; 
String room5 = null; 
String room6 = null; 
String room7 = null; 

또는 초기화

0

""에 당신은 단지 room1에 값을 제공하기 위해 보장하기 때문에 기본적으로 이런 일이 발생.

나머지 부분에는 if 문 내부의 값이 지정됩니다. 그러나 if 진술이 절대로 true 일 경우 어떻게해야합니까? room2,...,room7의 값은 무엇입니까?

그 이유는 compliler가 불평하고 있습니다. 모든 변수를 빈 String (
String room2 = "" 등)으로 초기화하면 문제가 없습니다.

0

컴파일 타임에 런타임시 예외/오류를 만들지 않도록 컴파일러가 제대로 초기화 된 경우 모든 변수 등을 검사합니다. 여기에는 런타임시 Nullpointerexception이 발생할 수있는 가능성이 있습니다. 따라서 이러한 변수를 값이나 빈 문자열로 초기화해야합니다.

관련 문제