声明变量并稍后在C#中使用它们

时间:2018-10-24 00:31:20

标签: c# console

我正在用C#制作基于文本的冒险游戏,我希望用户的游戏竞赛始终显示在顶部。当用户输入种族时,我希望它能够识别他们选择了什么种族,而不管他们如何输入。例如:用户输入oorc,输出为Orc。”

我现在有了它,可以根据用户输入的内容将变量player_race_display设置为某个值,但不允许我稍后使用该变量在代码中实际打印该值。

    static void Main(string[] args)

    {

        string player_race_display;

        //START SCREEN


        //Horizontal borders
        Console.SetCursorPosition(25, 5);
        Console.Write(" __________________________________________________________________");
        Console.SetCursorPosition(25, 18);
        Console.Write("___________________________________________________________________");

        //Vertical borders
        for (int c = 6; c <= 18; c++)
        {
            Console.SetCursorPosition(25, c);
            Console.Write("|");
            Console.SetCursorPosition(92, c);
            Console.Write("|");
        }

        //Asking for User Input
            //Asking Name and Giving Example
        Console.SetCursorPosition(30, 7);
        Console.Write("Enter Name: ");
        Console.SetCursorPosition(65, 7);
        Console.Write("\"Johnny Appleseed\"");
            //Asking Race and Giving Options
        Console.SetCursorPosition(30, 9);
        Console.Write("Enter Race: ");
        Console.SetCursorPosition(65, 9);
        Console.Write("(O)rc (E)lf (H)uman");
            //Put in code to select attributes based on race picked (eg strength, intelligence, etc)



        //UserInputs
            //name
        Console.SetCursorPosition( 42, 7);
        string player_name = Console.ReadLine();
            //race
        Console.SetCursorPosition(42, 9);
        string player_race = Console.ReadLine();



        if (player_race == "o" || player_race == "O" || player_race == "orc" || player_race == "Orc") 
            {
                player_race_display = "Orc";
            }
        else if(player_race == "e" || player_race == "E" || player_race == "elf" || player_race == "Elf")
            {
                player_race_display = "Elf";
            }
        else if (player_race == "h" || player_race == "H" || player_race == "human" || player_race == "Human")
            {
                 player_race_display = "Human";
            }



        Console.ReadKey();
        Console.Clear();

        //VISUALS WINDOW
            //Horizontal borders
        Console.SetCursorPosition(5,0);
        Console.Write(" _____________________________________________________________________________________________________________");
        Console.SetCursorPosition(5, 22);
        Console.Write("______________________________________________________________________________________________________________");

            //Vertical borders
        for (int a = 1; a <= 22; a++)
        {
            Console.SetCursorPosition(5, a);
            Console.Write("|");
            Console.SetCursorPosition(115, a);
            Console.Write("|");
        }

            //Border around top info
        Console.SetCursorPosition(5, 2);
        Console.Write(" _____________________________________________________________________________________________________________");

            //Name
        Console.SetCursorPosition(15, 1);
        Console.Write("Name: " + player_name);

            //Race
        Console.SetCursorPosition( 55, 1);
        Console.Write("Race: " + player_race_display);


        //--------------------------------------------------------------------------------------------------------------
        //GUI

        //Horizontal borders
        Console.SetCursorPosition(5, 23);
        Console.Write(" _____________________________________________________________________________________________________________");
        Console.SetCursorPosition(5, 28);
        Console.Write("______________________________________________________________________________________________________________");

        //Vertical borders
        for (int b = 24; b<=28; b++)
        {
            Console.SetCursorPosition(5, b);
            Console.Write("|");
            Console.SetCursorPosition(115, b);
            Console.Write("|");
        }




        Console.ReadKey();
    }
}

}

在这里我声明每个变量:

Console.SetCursorPosition(42, 9);
string player_race = Console.ReadLine();



if (player_race == "o" || player_race == "O" || player_race == "orc" || player_race == "Orc") 
{
    player_race_display = "Orc";
}
else if(player_race == "e" || player_race == "E" || player_race == "elf" || player_race == "Elf")
{
    player_race_display = "Elf";
}
else if (player_race == "h" || player_race == "H" || player_race == "human" || player_race == "Human")
{
    player_race_display = "Human";
}

这是我尝试使用变量打印选择的地方:

//Race
Console.SetCursorPosition( 55, 1);
Console.Write("Race: " + player_race_display);

我收到消息:

  

使用未分配的局部变量“ player_race_display”

任何帮助都非常感谢,因为我是编程的新手。预先感谢。

1 个答案:

答案 0 :(得分:2)

收到此错误消息的原因是因为并非每个路径在使用前都为player_race_display分配了一个值。

考虑以下代码:

string value;
int i = 2;
if (i == 0) 
{
    value = "Zero";
}
else if (i == 1)
{
    value = "One";
}
Console.WriteLine("Value is " + value);

显然value永远不会被赋值。

有两种解决方法:

  1. 在定义默认值时:

    string value = "defaultValue";

  2. 通过添加else在所有代码路径中分配值:


string value;
int i = 2;
if (i == 0) 
{
    value = "Zero";
}
else if (i == 1)
{
    value = "One";
}
else
{
    value = "ElseReached";
}
Console.WriteLine("Value is " + value);

**请注意,您分配的默认值可以为null,但请注意不要给自己造成错误。