JOptionPane变量可能尚未初始化

时间:2014-01-18 04:00:11

标签: java swing compiler-errors joptionpane

我很抱歉代码太长了,我只需要知道我缺少什么来初始化我的所有决定。编译器抱怨我的变量未被初始化。谢谢。

import javax.swing.JOptionPane;

public class SemesterProject
{

public static void main(String[] args)

{

   String SemesterProject =
"Hello and welcome to Juans adventure story! Your choices throughout this story will bring your doom or your salvation!\n"+
"No one has been able to complete this story and lived to tell the tale, but maybe you will be the first one!.\n"+

"Our story begins in the small town of Badow. Many people in the town spread rumors of brave adventurers that\n"+
"go on the quest to solve the mysterious riddles of the abandond castle at the top of the hill.\n"+
"No one really knows who or what lies at the heart of the castle. Legend says that monsters guards the\n"+
"the many riches once owned by the family that died there, so many years ago.\n"+

"The Castle is full of dizzing turns and hidden corridors and dangerous monsters. Many adventureres \n"+
"have gone insane because of how they lose their way or go get stuck. It is the hardes quest known to man.\n"+
"The quest for you is to go to the castle, solve the riddles and claim the riches.\n"+
"The world will little note, nor long remember what we say here, but it can never forget what they did here.\n"+
"Be careful. Watch your back and always mark your trail because you never know what you will find next.\n"+
"If for some crazy reason you end up successful in this task, you will not only get the riches in the castle but\n"+
"you will also be crowned the king of the town of Badow. If you die, you lose.\n"+

"The last thing you have to do is input some of your personal information so that we keep you in our records.\n";


JOptionPane.showMessageDialog(null,SemesterProject);


String name = JOptionPane.showInputDialog(null,"Please give us your first name.");

String age = JOptionPane.showInputDialog(null,"Please give us your age.");

String weight = JOptionPane.showInputDialog(null,"Please give us your weight.");

String haircolor = JOptionPane.showInputDialog(null,"Please give us your hair color.");

String GPA = JOptionPane.showInputDialog(null,"Please give us your GPA.");

String favoritecolor = JOptionPane.showInputDialog(null,"Please give us your favorite color.");

String decision1,decision2,decision3,decision4,decision5,decision6;

decision1 = JOptionPane.showInputDialog("Brave adventurer, welcome to the entrance of the abandoned castle of town Badow. At this moment\n"+
"You have a choice right now to give up and take a walk of shame back to the village, and to safety\n"+
"Or you can decide to go in and test your unique set of skills on the various challenges of the adventure\n"+
"ahead of you. Enter the number of your choice.You can either 1. Enter the castle 2. Stay behind.");

    if (decision1.equals("1"))
    {
        decision2 = JOptionPane.showInputDialog
        ("You find yourself at the entrance of the castle with a choice to go down a dark hall\n"+
        "or to the closest room on the right side. Be vary wary the darkness can make you lose your mind and go insane and remember\n"+
        "to keep track of were you are. 1. Into the room 2. Down the hall.");

        }
        else
        {
             JOptionPane.showMessageDialog(null,"You go back to your boring life in the town, you wont be remembered by anything and you\n"+
             "lost your chance at getting the riches that lie deep in the castle. You lose.");
    }
    if (decision2.equals("1"))
    {
        decision3 = JOptionPane.showInputDialog
        ("You step into the room and find some medication and a lantern with some oil.\n"+
        "you also find a sword. You can choose now to go back to the dark hallway or to go down a hidden trap door in the room. It could be\n"+
        "a shortcut or it could lead somewhere dangerous. 1. Back down the hall 2. Take your chances with the trapdoor.");

            }
            else
            {
                JOptionPane.showMessageDialog(null,"you should have looked in the room, you quickly lose your way and the darkness\n"+
                "makes you crazy.");
    }
    if (decision3.equals("1"))
    {
            decision4 = JOptionPane.showInputDialog
            ("You are back in the hall slowly hearing the echoing sound of your footsteps\n"+
            "you head downstairs into what seems to be another hallway but this one full of barred rooms, kind of like a dungeon. There is\n"+
            "a skeleton body on the ground which seems to be of another failure by an adventurer in the castle. You reach the end of the hall\n"+
            "and there is two ways to go left or right. 1. Left 2. Right.");

        }
        else
        {
             JOptionPane.showMessageDialog(null,"You squeeze through the trapdoor and fall down to the ground where  you find youself lost and disoriented\n"+
             "You dont know where you are and start to panic, you start becoming insane and die.");
            }
            if (decision4.equals("1"))
            {

                   decision5 = JOptionPane.showInputDialog
                   ("You go left and hear a door open behind you, you panic and start to run\n"+
                   "you run intro different halls to try and escape but you trip. You just remembered that you have a sword with you and can now\n"+
                   "choose what to do 1. Take out sword and fight enemy 2. Get up and keep running.");
               }
               else
               {
                       JOptionPane.showInputDialog(null,"You go right and get locked in behind you. You realize that there isnt anwhere to go\n"+
                       "and get lost in the darkest part of the castle and die alone.");

            }
            if (decision5.equals("1"))
            {

                decision6 = JOptionPane.showInputDialog
("You were able to fight of the armored enemy. But something tells you that that isnt the last of them. You continue on down the\n"+"hall and see from the distance to what seems to be your first prize. It cant be that easy.");

        }
    }
}

4 个答案:

答案 0 :(得分:0)

您必须将String个变量(decision1decision2,...)初始化为:

String decision1 = null, decision2 = null, decision3 = null, decision4 = null, decision5 = null, decision6 = null;

请注意,decision1没有出现编译错误,因为在

行中
decision1 = JOptionPane.showInputDialog(...);

正在初始化。

另外,你的程序逻辑很糟糕。如果decision1不是1,那么您只会显示一条消息,但程序会继续检查下一个条件:

if (decision2.equals("1"))

您将获得NullPointerException,因为您要将null1进行比较。

答案 1 :(得分:0)

将决策变量定义为null。而不是:

String decision1,decision2,decision3,decision4,decision5,decision6;

将变量定义为:

String decision1 = null;
String decision2 = null;
String decision3 = null;
String decision4 = null;
String decision5 = null;
String decision6 = null;

答案 2 :(得分:0)

只有在decision1等于1时才设置decision2。我认为你想把其他决策的if语句放在彼此之内,如下所示:

if (decision1.equals("1"))
{
    decision2 = JOptionPane.showInputDialog
    ("You find yourself at the entrance of the castle with a choice to go down a dark hall\n"+
    "or to the closest room on the right side. Be vary wary the darkness can make you lose your mind and go insane and remember\n"+
    "to keep track of were you are. 1. Into the room 2. Down the hall.");

    if (decision2.equals("1"))
    {
    decision3 = JOptionPane.showInputDialog
    ("You step into the room and find some medication and a lantern with some oil.\n"+
    "you also find a sword. You can choose now to go back to the dark hallway or to go down a hidden trap door in the room. It could be\n"+
    "a shortcut or it could lead somewhere dangerous. 1. Back down the hall 2. Take your chances with the trapdoor.");

        }
        else
        {
            JOptionPane.showMessageDialog(null,"you should have looked in the room, you quickly lose your way and the darkness\n"+
            "makes you crazy.");
}
    }
    else
    {
         JOptionPane.showMessageDialog(null,"You go back to your boring life in the town, you wont be remembered by anything and you\n"+
         "lost your chance at getting the riches that lie deep in the castle. You lose.");
}

答案 3 :(得分:0)

JOptionPane variable might not have been initialized

因此,请记住,每当您收到此类错误消息时,请尝试查看您的变量是否已正确声明。

当您尝试使用int变量时,如果在某些计算中使用该变量,它可以为您提供错误消息。因此,尝试初始化变量,如:

int a=0;

您可以使用字符串变量执行相同的操作,例如:

String a=null;

回复

在您的代码中,您刚刚声明了变量,但事实是您必须实际初始化它们。

替换

String decision1,decision2,decision3,decision4,decision5,decision6;

通过

String decision1=null,decision2=null,decision3=null,decision4=null,decision5=null,decision6=null;