-1

同じ文字列変数の複数のエントリをループできる方法があるかどうか興味がありますか? 基本的に、プレイヤー名の複数のエントリを For ループに出力したいのですが、頭がおかしくなり、これまでに書いた単純なコードを while で拡張する方法に困惑しています。 -ループ。

    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 に答える