0

我正在尝试从另一个类中调用一个方法。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;

这是我在 Player 类中的方法:

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();
    }
4

2 回答 2

1

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

你的意思是以下吗?

string enterplayer = "";
while (enterplayer != "Y")
{
    ...
}
于 2013-07-30T17:56:09.043 回答
0

您编写的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构造的变体,它可能比前者使用得更少。

于 2013-07-30T18:17:45.793 回答