0

为了练习使用 if else、do while 和 switch 语句,我正在制作一个小型文本冒险游戏,用户可以在其中输入他们的名字,获得随机生成的职业,并被分配随机生成的任务。然而,在第二个目标进行到一半时,我使用的 java 开发程序不断说我的一个变量“可能没有被初始化”。

到目前为止,这就是我所拥有的代码:

=============

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

        String name = "";

        char userInput;
        char userYN;
        char user2YN;

        int dieRoll = (int) (Math.random() * 9);
        char outputType;

        char Mage;
        char Soldier;
        char Explorer;
        char howTo;

        //exeternal documation
        System.out.println("The First Adventure by K. Konieczny ");
        System.out.println();

        //player name
        do
        {
            System.out.println();
            System.out.print("What is your name: ");
            name = keyboard.nextLine();
            //prompt
            System.out.print("So your name is " + name + "? Are you sure y/n : ");
            userYN = keyboardYN.nextLine().charAt(0);
            System.out.println();
            if(userYN == 'y')
            {
                System.out.println();
            }
            else
            {
                System.out.println("Type in your real name.");
            }

            }//end do
        while(userYN == 'n');

        //narration pt. 1
        System.out.println("You, " + name +
                           " have just been named the greatest, uh, what was it again?");
        System.out.println();

        //specialization
        System.out.print("Roll the dice to decide what your profession is? y/n : ");
        user2YN = keyboard2YN.nextLine().charAt(0);
        if(user2YN == 'y')
           {
            switch (dieRoll)
            {
                case '0':
                case '1':
                case '2': outputType = Mage;
                case '3':
                case '4':
                case '5': outputType = Soldier;
                case '6':
                case '7':
                case '8': outputType = Explorer;
                default : outputType = howTo;
            }//end switch
            System.out.println("Oh right, you are the greatest " + outputType + " in the town.");

            }
            else
            {
                System.out.println("I must be thinking of someone else then.");
            }

        //get quest


        System.out.println();
        System.out.println("End of program");
    }//end main
}//end class

=============

我收到的错误消息是“变量 Mage 可能尚未初始化”。

我没有太多的编码经验,并且想知道我做错了什么以及如何在以后的程序中修复它。

4

1 回答 1

1

你有:

char Mage;
// ...
case '2': outputType = Mage;

那个时候的价值是Mage多少?编译器警告您该变量尚未初始化。

您可能希望初始化Mage为某个值,例如:

char Mage = '0';

或者很可能您想要String表示Mage

String outputType;
String mage = "Mage";
String soldier = "Soldier";
String explorer = "Explorer";
// ...
switch (dieRoll) {
    case '0':
    case '1':
    case '2': outputType = mage; 
              break;
    case '3':
    case '4':
    case '5': outputType = soldier; 
              break;
    case '6':
    case '7':
    case '8': outputType = explorer;
              break;
    default : outputType = "Oops";
}
于 2015-09-22T22:06:58.243 回答