C#分配给公共类和列表中的字符串
到目前为止,我的代码执行以下操作C#分配给公共类和列表中的字符串,c#,class,C#,Class,到目前为止,我的代码执行以下操作 要求用户提供“玩家”的数字金额 然后询问添加到列表和类中的每个玩家的姓名 我想从列表或类中调用这些名称(不确定类如何工作)并将其分配给一个新字符串。以下是我目前得到的信息: public class NameVariable { public int ID { get; set; } public string Name { get; set; } } class Program { static void Main(string[]
public class NameVariable
{
public int ID { get; set; }
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
bool IsUserWrong = false;
Console.WriteLine("Write amount of players");
while (!IsUserWrong)
{
int TotalPlayers;
while (!Int32.TryParse(Console.ReadLine(), out TotalPlayers))
{
Console.WriteLine("Value must be numeric.");
}
if (TotalPlayers >= 12 && TotalPlayers <= 16)
{
List<NameVariable> PlayerList = new List<NameVariable>();
for (int index = 0; index < TotalPlayers; index++)
{
Console.WriteLine("Enter player {0}'s name:", index + 1);
PlayerList.Add(new NameVariable
{
Name = Console.ReadLine(),
ID = index
});
}
// string player1 = ???
// string player2 = ???
// and so on for 12-16 players
}
else
{
Console.WriteLine("Please enter a value between 12 and 16.");
}
}
}
}
提前谢谢 只是
string player1 = PlayerList[0].Name;
string player2 = PlayerList[1].Name;
...
本质上,您的列表包含NameVariable对象。PlayerList[index]提供对象,而.Name提供对象的属性值
如果您想通过特定ID号获得特定的玩家名称,可以使用LINQ(只是为了给您一个提示)
虽然您眼前问题的答案,即如何访问类对象的属性,如其他人所示,但我觉得这段代码有一个更大的问题。也就是说,您试图在一个函数中执行太多操作,即,
Main()
。因此,我建议您尝试重构您的代码,让一个函数做一件事。比如:
public static int GetNumberOfPlayers()
{
Console.Write("Enter number of players: ");
int totalPlayers;
while (!Int32.TryParse(Console.ReadLine(), out totalPlayers))
{
Console.WriteLine("Value must be numeric.");
}
return totalPlayers;
}
public static List<NameVariable> GetPlayerList(int num)
{
var list = new List<NameVariable>();
for (int i = 0; i < num; i++)
{
Console.WriteLine("Enter player {0}'s name:", i + 1);
list.Add(new NameVariable
{
Name = Console.ReadLine(),
ID = i
});
}
return list;
}
public static void DisplayPlayers(List<NameVariable> list)
{
foreach(var player in list)
{
Console.WriteLine("Player {0}, Name: {1}", player.ID, player.Name);
}
}
public static void CantThinkOfAGoodName()
{
while (true)
{
int totalPlayers = GetNumberOfPlayers();
if (totalPlayers > 16 || totalPlayers < 12)
{
Console.WriteLine("Please enter a value between 12 and 16.");
}
else
{
var playerList = GetPlayerList(totalPlayers);
DisplayPlayers(playerList);
break;
}
}
}
public static void Main()
{
CantThinkOfAGoodName();
Console.ReadLine();
}
public static int GetNumberOfPlayers()
{
控制台。写入(“输入玩家数量:”;
国际球员;
而(!Int32.TryParse(Console.ReadLine(),out totalPlayers))
{
WriteLine(“值必须是数字。”);
}
返回所有玩家;
}
公共静态列表GetPlayerList(int num)
{
var list=新列表();
for(int i=0;i16 | | TotalPlayer<12)
{
Console.WriteLine(“请输入一个介于12和16之间的值”);
}
其他的
{
var playerList=GetPlayerList(totalplayerlist);
显示玩家(玩家列表);
打破
}
}
}
公共静态void Main()
{
CantGoodName();
Console.ReadLine();
}
不确定是否有帮助,但您可以使用索引器按姓名获取玩家
public NameVariable this[string name]
假设您为集合创建了一个类
public class NameColection : List<NameVariable>
{
public NameVariable this[string name]
{
get
{
return this.FirstOrDefault(n => n.Name == name);
}
}
}
作为
列表
中的nameCollection,您将能够以通常的方式添加、删除或修改项目。您不需要这样做。只需使用集合,集合应该替换这样的编号变量;现在不需要它们。虽然这都是事实,但应该指出的是,基于某些索引对其进行硬编码首先会破坏使用集合的目的。我采纳了您的建议,将主要部分拆分为单独的部分!也有助于反复使用相同的代码。
public NameVariable this[string name]
public class NameColection : List<NameVariable>
{
public NameVariable this[string name]
{
get
{
return this.FirstOrDefault(n => n.Name == name);
}
}
}
var players = new NameColection()
{
new NameVariable() { ID = 1 , Name = "John" },
new NameVariable() { ID = 2 , Name = "Paul" },
new NameVariable() { ID = 3 , Name = "George" },
new NameVariable() { ID = 4 , Name = "Ringo" }
};
var player1 = players["John"];