0

我正在尝试在 C# 中创建一个文本冒险,它与 XNA 编码非常不同,我正在尝试显示玩家的性别,但它在不同的领域。我收到此错误:

当前上下文中不存在名称“BoyorGirl”

(错误在第 113 行,第 73 列)。

这是脚本:(我在出现错误的地方放了一个//)

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ....
            Console.WriteLine("\t\t    Press Enter to continue...");
            Console.ReadLine();
            Console.Clear();
            Start_Game();
        }

        static public void Start_Game()
        {
            int StartMenu;
            Console.WriteLine(@"Welcome Adventurer..
            Are you ready for an adventure?
            #1 Start Game
            #2 Help
            #3 Exit");
            StartMenu = int.Parse(Console.ReadLine());
            switch (StartMenu)
            {
                case 1:
                    Start_Adventure();
                    break;
                case 2:
                    Help_Menu();
                    break;
                case 3:
                    Console.WriteLine("Goodbye.");
                    System.Threading.Thread.Sleep(1000);
                    Environment.Exit(0);
                    break;
                default:
                    Console.WriteLine("This is not an option..");
                    System.Threading.Thread.Sleep(2000);
                    Console.Clear();
                    Start_Game();
                    break;
            }
        }

        static public void Start_Adventure()
        {
            Console.Clear();
            Console.WriteLine("You're a normal..\nBoy/Girl?");
            string BoyorGirl;
            BoyorGirl = Console.ReadLine();
            BoyorGirl = BoyorGirl.ToLower();
            switch (BoyorGirl)
            {
                case "boy":
                    BoyorGirl = "Boy";
                    Console.Clear();
                    input_name();
                    break;
                case "girl":
                    BoyorGirl = "Girl";
                    Console.Clear();
                    input_name();
                    break;
                default:
                    Console.WriteLine("This is not an option..");
                    System.Threading.Thread.Sleep(2000);
                    Console.Clear();
                    Start_Adventure();
                    break;

            }

        }

        public static void input_name()
        {
            Console.WriteLine("You're just a normal {0}, called.. input your name please.");
            string name;
            name = Console.ReadLine();
            Console.Clear();
            Console.WriteLine("You're just a normal {0}, called {1}..", BoyorGirl, name); //The error appears here.
        }

        static public void Help_Menu()
        {

        }
    }
}

我也想摆脱静态,但我认为这是不可能的。关于如何解决这个问题的任何想法?

4

5 回答 5

1

该变量仅存在于Start_Adventure()方法的范围内。

如果要在该方法中使用它,则需要全局删除它或将其传递给该方法。

public static void input_name(string BoyorGirl)
{
    //Do Work Here
}

或者

namespace ConsoleApplication1
{
    class Program
    {
         private static string BoyOrGirl;
    ...

如果您选择第二个选项,请记住将其删除

string BoyorGirl;

Start_Adventure()

于 2013-11-14T09:48:47.493 回答
0

因为BoyorGirl是您Start_Adventure方法的局部变量。如果要使用它,则需要传递该变量:

    BoyorGirl = "Girl";
    Console.Clear();
    input_name(BoyorGirl);

// ....
public static void input_name(string BoyorGirl) {
}
于 2013-11-14T09:50:08.997 回答
0

将您的代码更改为以下内容:

    static public void Start_Adventure()
    {
        ...
        input_name(BoyorGirl);
        ...
    }

    public static void input_name(string BoyorGirl)
    {
        Console.WriteLine("You're just a normal {0}, called.. input your name please.");
        string name;
        name = Console.ReadLine();
        Console.Clear();
        Console.WriteLine("You're just a normal {0}, called {1}..", BoyorGirl, name); //The error appears here.
    }

您的 - 方法中不知道字符串 BoyorGirl input_name

所以你必须将字符串传递给方法或使用全局变量。

于 2013-11-14T09:50:10.067 回答
0

BoyorGirl您在方法的范围内定义了变量Start_Adventure(),它在该范围之外将不可用。因此,您必须将变量更改为 class 的成员,或者将其作为参数Program传递给方法。input_name()

于 2013-11-14T09:50:55.493 回答
0

在方法中声明的变量是该特定方法的本地变量,不能从其他地方访问。因此,input_name不知道是什么BoyorGirl,因为它只在Start_Adventure.

您可以做的是将变量传递给input_name

public static void input_name(string BoyorGirl)

input_name(BoyorGirl);
于 2013-11-14T09:52:03.947 回答