java - "Variable might not have been initialized"-- Java 中基于文本的冒险

标签 java variables initialization

所以我正在尝试创建一个基于文本的冒险。它从一个房间开始,并为您提供两个房间选择。从那里开始,每个房间选项还有另外 2 个选项。然后每个房间都有一个"is"或“否”选项..

共有 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!!! " );
    }
}

}

最佳答案

初始化变量:

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;

或初始化为""

关于java - "Variable might not have been initialized"-- Java 中基于文本的冒险,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25836068/

相关文章:

swift - 将 [Float] 转换为变量参数

python - python中的变量前单 Asterisk 和双 Asterisk 有什么区别?

string - 字符串内的换行符显示在 TMemoBox 上

c++ - 使用带有虚函数的类的构造函数进行大括号初始化

C++ 初始化列表

c++ - 在编译或初始化时对长数组进行编程初始化

java - 通过代码编辑Linux网络配置文件("/etc/network/interfaces")

java - Android : com. google.maps :google-maps-services:0. 1.7 给出构建错误

java - 鉴别器公式

java - 我的验证全部合一