C# 试图打印来自同一变量的多个条目
我很好奇是否有一种方法可以循环同一字符串变量的多个条目?基本上,我想把玩家名字的多个条目打印到一个For循环中,但是我有一个头脑发热的问题,我很难理解如何扩展我迄今为止用while循环编写的简单代码C# 试图打印来自同一变量的多个条目,c#,for-loop,while-loop,C#,For Loop,While Loop,我很好奇是否有一种方法可以循环同一字符串变量的多个条目?基本上,我想把玩家名字的多个条目打印到一个For循环中,但是我有一个头脑发热的问题,我很难理解如何扩展我迄今为止用while循环编写的简单代码 class Program { static void Main(string[] args) { string choice = "y"; string player_name = ""; while (choice == "y") {
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();
}
}
将名称放入列表中,然后在列表中循环:
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);
}
List player_names=new List();
做{
Console.WriteLine(“输入玩家名称:”);
player_name=Console.ReadLine();
玩家名称。添加(玩家名称);
Console.WriteLine(“是否要输入其他播放器?是/否”);
}而(Console.ReadLine()=“y”);
foreach(播放器名称中的字符串名称){
Console.WriteLine(名称);
}
在while循环中放置WriteLine(玩家名称)无效?列表!我还没有用过这个,但这正是我需要它做的。谢谢你的快速回复。