그래서 텍스트 기반 모험을 만들려고합니다. 방에서 시작하여 두 가지 방 옵션을 제공합니다. 거기에서 각 방 옵션에는 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!!! ");
}
}
}
단순 : 컴파일러에서 불평하는 변수를 초기화하십시오. * 일부 값, 문자열 일 경우'null' 또는''''중 하나를 지정하십시오. –
개념 상 오류가 발생했습니다 : 사용자가 "위층에"입력하지 않았을 때'if (room2.equals ("up"))'가 쉽게'NullPointerException'을 일으킬 수 있습니다. 사용자가 "지하실"과 다른 것을 입력하면 'room3'도 마찬가지입니다. – Tom
답변을 찾은 경우 질문 제목을 수정하지 마십시오. 대신 투표 카운터 아래의 체크 표시를 클릭하여 해당 답변을 "수락 됨"으로 표시하십시오. – Tom