来自另一个类的方法没有';不要打电话

本文关键字:打电话 另一个 方法 | 更新日期: 2023-09-27 18:25:29

我正试图从另一个类调用一个方法。当我选择以下菜单选项时,p.players()应该打开:

 static void Main(string[] args)
    {
        Enumfactory.Position choice;
        Enumfactory.Location location;
        Player p = new Player();

        Console.WriteLine("Please choose from one of the following:");
        Console.WriteLine("1. GoalKeeper");
        Console.WriteLine("2. Defender");
        Console.WriteLine("3. Midfielder");
        Console.WriteLine("4. Striker");
        choice = ((Enumfactory.Position)(int.Parse(Console.ReadLine())));
        string exit = "";
        while (exit != "Y")
        {
            switch (choice)
            {
                case Enumfactory.Position.GoalKeeper:
                    //assigning the actual position
                    p.Position = Enumfactory.Position.GoalKeeper;
                    p.players();
                    break;

这是我在类播放器中的方法:

public string[] players()
    {
        List<string> PlayerList = new List<string>();
        Player player = new Player();
        string enterplayer = "";
        while (enterplayer == "Y")
        {
            Console.WriteLine("Please enter the teamnumber of your player");
            player.teamNumber = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter the name of your player");
            player.name = Console.ReadLine();
            Console.WriteLine("Please enter the surname of your player");
            player.surname = Console.ReadLine();
            Console.WriteLine("Enter the age of your player");
            player.age = int.Parse(Console.ReadLine());
            Console.WriteLine("Please enter the goals the player scored");
            player.goalsScored = int.Parse(Console.ReadLine());
            PlayerList.Add(player.teamNumber.ToString());
            PlayerList.Add(player.name);
            PlayerList.Add(player.surname);
            PlayerList.Add(player.age.ToString());
            PlayerList.Add(player.goalsScored.ToString());
            Console.WriteLine("Do you wish to enter another player? Y/N");
            enterplayer = Console.ReadLine();
        }
        foreach (var item in PlayerList)
        {
            Console.WriteLine("to view your player");
            Console.Write("{0}", item);
        }
        Console.ReadKey();
        return player.players();
    }

来自另一个类的方法没有';不要打电话

该方法可能正在被调用,它只是您的while循环从未运行过。这是因为enterplayer永远不会等于"Y",因此while循环中的代码永远不会运行(这使您的方法看起来没有被调用)。

你是指以下内容吗?

string enterplayer = "";
while (enterplayer != "Y")
{
    ...
}

while循环按照您编写它的方式,将在第一次进入循环之前评估条件。您将enterplayer初始化为"",因此第一次测试while条件时,它将返回false,并且永远不会进入循环。您可以通过两种方式解决此问题,一种是初始化enterplayer,以便通过首次满足条件

string enterplayer = "Y";
while (enterplayer == "Y") // We set enterplayer to "Y" so this is true first time through
{
    // Your code to add a player goes here
}

或者,您可以使用稍微不同形式的while循环,在该循环中,条件将在末尾求值。这意味着循环中的代码总是至少执行一次,然后只要满足末尾的while条件就重复执行:

string enterplayer = "";
do // This always enters the loop code first time
{
    // Your code to add a player goes here
}
while (enterplayer == "Y")

由于您的代码使用enterplayer变量来决定是否添加更多的播放器,因此我更喜欢第二种形式,尽管作为while结构的变体,它可能比前者使用更少。