【问题标题】:How do I access this list in C# in other functions?如何在 C# 中的其他函数中访问此列表?
【发布时间】:2020-06-22 09:42:57
【问题描述】:

我正处于创建人们投注马匹的游戏的早期阶段。我需要访问“BetOnARace”函数中的“HorseList”。这是我的尝试,但在“程序”类中的“主要”一词的末尾附近有一条错误消息,它说它“作为入口点的签名错误”。

class RunGame
{

    public void StartMenu(List<Horse> HorseList)
    {
        Console.WriteLine("**********    MENU     **********");
        Console.WriteLine("*********************************");
        Console.WriteLine("1. LOAD GAME");
        Console.WriteLine("2. NEW GAME");
        Console.WriteLine("3. EXIT GAME");
        Console.WriteLine("Enter your choice: ");
        string choice = Console.ReadLine();
        if (choice == "3")
        {
            Environment.Exit(0);
        }
        if (choice == "1" || choice == "2")
        {
            StartingBalance(HorseList);
        }
    }

    public void StartingBalance(List<Horse> HorseList)
    {
        int balance;
        Console.WriteLine("****    STARTING BALANCE     ****");
        Console.WriteLine("*********************************");
        Console.WriteLine("1. DEFAULT (£500)");
        Console.WriteLine("2. CUSTOM");
        Console.WriteLine("3. BACK TO MAIN MENU");
        Console.WriteLine("Enter your choice: ");
        string choice = Console.ReadLine();
        if (choice == "1")
        {
            balance = 500;
            GameMenu(HorseList);
        }
        if (choice == "2")
        {
            Console.WriteLine("Enter balance: ");
            Console.Write("£");
            balance = Convert.ToInt32(Console.ReadLine());
            if (balance <= 0)
            {
                Console.WriteLine("Balance must start above £0");
                Console.WriteLine("Enter balance: ");
                Console.Write("£");
                balance = Convert.ToInt32(Console.ReadLine());
                GameMenu(HorseList);
            }
            else
            {
                GameMenu(HorseList);
            }
        }
        if (choice == "3")
        {
            RunGame rg = new RunGame();
            rg.Run(HorseList);
        }
    }


    public void GameMenu(List<Horse> HorseList)
    {
        Console.WriteLine("**********    MENU     **********");
        Console.WriteLine("*********************************");
        Console.WriteLine("1. BET ON A RACE");
        Console.WriteLine("2. VISIT STABLES");
        Console.WriteLine("3. EXIT TO START MENU");
        Console.WriteLine("Enter your choice: ");
        string choice = Console.ReadLine();
        switch (choice)
        {
            case "1":
                BetOnRace(HorseList);
                break;
            case "2":
                DisplayHorses();
                break;
            case "3":
                Console.WriteLine("GAME PROGRESS SAVED");
                RunGame rg = new RunGame();
                rg.Run(HorseList);
                break;
        }
    }

    public void DisplayHorses()
    {
        List<Horse> HorseList = new List<Horse>();
        HorseList.Add(new Horse("Purple Haze", 2, 0, 0));
        HorseList.Add(new Horse("Druggo", 3, 0, 0));
        HorseList.Add(new Horse("Mr Crowley", 7, 0, 0));
        HorseList.Add(new Horse("I Am Monky", 10, 0, 0));
        HorseList.Add(new Horse("Roy", 12, 0, 0));
        HorseList.Add(new Horse("Egg Mayo", 14, 0, 0));
        HorseList.Add(new Horse("Crash My Horse Into A Bridge", 17, 0, 0));
        HorseList.Add(new Horse("Chubungundy", 20, 0, 0));
        HorseList.Add(new Horse("Uncle Barry", 22, 0, 0));
        HorseList.Add(new Horse("Widgetygrub", 2, 0, 0));
        HorseList.Add(new Horse("Paid In Raisins", 8, 0, 0));
        HorseList.Add(new Horse("Crayzee", 15, 0, 0));
        HorseList.Add(new Horse("Camster", 25, 0, 0));
        HorseList.Add(new Horse("Cheese N Peas", 18, 0, 0));
        HorseList.Add(new Horse("Master Oogway", 20, 0, 0));
        HorseList.Add(new Horse("Danny DeVito", 11, 0, 0));
        HorseList.Add(new Horse("Yobungus", 4, 0, 0));
        HorseList.Add(new Horse("Hot Cheetos", 14, 0, 0));
        HorseList.Add(new Horse("Bobby", 22, 0, 0));
        HorseList.Add(new Horse("This Horse Will Win", 50, 0, 0));

        Console.WriteLine("********    STABLES     *********");
        Console.WriteLine("*********************************");
        Console.WriteLine("1. DISPLAY HORSES");
        Console.WriteLine("2. BUY HORSE");
        Console.WriteLine("3. SELL HORSE");
        Console.WriteLine("4. BACK");
        Console.WriteLine("Enter your choice: ");
        string choice = Console.ReadLine();
        switch (choice)
        {
            case "1":
                foreach (var horse in HorseList)
                {
                    Console.WriteLine("Name of Horse: {0}, Odds: {1}/1, Races: {2}, Wins: {3}", horse.GetHorseName, horse.GetOdds, horse.GetRaces, horse.GetWins);
                }
                DisplayHorses();
                Console.ReadLine();
                break;
            case "2":
                break;
            case "3":
                break;
            case "4":
                GameMenu(HorseList);
                break;
        }
    }

    public void BetOnRace(List<Horse> HorseList)
    {
        Random rnd = new Random();
        HorseList.OrderBy(x => rnd.Next()).Take(5);
    }

    public void Run(List<Horse> HorseList)
    {
        StartMenu(HorseList);
    }
}
class Program
{
    static void Main(string[] args, List<Horse>HorseList)
    {
        RunGame rg = new RunGame();
        rg.Run(HorseList);
    }
}
}

【问题讨论】:

标签: c# list function


【解决方案1】:

错误是由于这一行:

static void Main(string[] args, List<Horse>HorseList)

您不能更改控制台应用程序的 Main 方法的签名,它只需要有一个 string[] 类型的参数(通常命名为 args,但可以命名为任何名称)。

但是,我可以看到你想要做什么。

你有一个班级RunGame,你应该做的是双重的

  1. 在这个类上添加一个构造函数,它获取你的马列表(或者,如果您更愿意在构造函数中实际生成列表)
  2. 将该列表存储在类的私有属性中

然后你可以在整个过程中使用它,而不必通过该类的每个方法。

class RunGame
{
     private readonly List<Horse> horsesList;

    public RunGame()
    {
         this.horseList = new List<Horse>
         {
           new Horse("Purple Haze", 2, 0, 0),
           new Horse("Druggo", 3, 0, 0),
           // .. etc .. //
         };
    }

    public void DisplayHorses()
    {
        foreach(var horse in horsesList)
        {
            // TODO: display details of horse
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 2016-04-29
    • 1970-01-01
    • 2017-04-27
    • 2015-07-11
    相关资源
    最近更新 更多