这个问题已经在这里有了答案:

为了练习使用if else,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可能尚未初始化”。

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

===============>>#1 票数:1

你有:

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

Mage的价值是什么? 编译器警告您该变量尚未初始化。

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

char Mage = '0';

或者最有可能您想要MageString表示形式:

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";
}

  ask by K.Konieczny translate from so

未解决问题?本站智能推荐: