-1

我很好奇是否有一种方法可以循环同一个字符串变量的多个条目?基本上,我想将玩家姓名的多个条目打印到一个 For 循环中,但是我有一个脑残,我很难理解如何扩展我到目前为止编写的简单代码段-环形。

    class Program
{
    static void Main(string[] args)
    {
    string choice = "y";
    string player_name = "";

    while (choice == "y")
    {

        Console.WriteLine("Enter the players name: ");
        player_name = Console.ReadLine();


        Console.WriteLine("Would you like to enter another player? y/n");
        choice = Console.ReadLine();

    }
    Console.WriteLine(player_name);

    Console.ReadLine();

    }
}
4

1 回答 1

2

将名字放在一个列表中,然后遍历列表:

List<string> player_names = new List<string>();

do {

    Console.WriteLine("Enter the players name: ");
    player_name = Console.ReadLine();
    player_names.Add(player_name);

    Console.WriteLine("Would you like to enter another player? y/n");
} while (Console.ReadLine() == "y");

foreach (string name in player_names) {
  Console.WriteLine(name);
}
于 2012-10-16T21:55:57.893 回答