C#如何从名单中找到球员,以便我可以打印出他们的分数
我做了一个游戏,你先说有多少玩家,然后每个玩家掷3个飞镖,然后循环直到一个玩家得到301分,现在我想从玩家列表中得到超过301分的玩家,我想得到他们的分数,这样我就可以打印出他们的分数,并说他们在游戏结束时赢了,但我我不知道该怎么做。谢谢C#如何从名单中找到球员,以便我可以打印出他们的分数,c#,list,C#,List,我做了一个游戏,你先说有多少玩家,然后每个玩家掷3个飞镖,然后循环直到一个玩家得到301分,现在我想从玩家列表中得到超过301分的玩家,我想得到他们的分数,这样我就可以打印出他们的分数,并说他们在游戏结束时赢了,但我我不知道该怎么做。谢谢 class Program { public static void Main(string[] args) { Game My_game = new Game(); My_game.PlayGame();
class Program
{
public static void Main(string[] args)
{
Game My_game = new Game();
My_game.PlayGame();
}
}
public class Game
{
private List<Player> player_list = new List<Player>();
private List<int> total_list = new List<int>();
public void PlayGame()
{
Random random_number = new Random();
int throw1;
int throw2;
int throw3;
string more_players = "yes";
while (more_players == "yes")
{
Console.WriteLine("What is the players name?: ");
player_list.Add(new Player(Console.ReadLine()));
Console.WriteLine("Are there more players?");
more_players = Console.ReadLine();
}
Console.WriteLine("\n\n Welcome to the dartgame! \n" +
"\n Game Rules: Each player throws 3 darts at a time." +
"\n Every throw can be worth 0-20 points." +
"\n Whoever gets 301 points first is the winner!");
Console.WriteLine("\nPlayers:");
foreach (var players in player_list)
{
Console.WriteLine(players);
}
int total_points = 0;
while (!player_list.Any(x => x.calculate_points() >= 301))
{
foreach (var players in player_list)
{
Console.WriteLine("\n\n\n\n");
Console.WriteLine("\n first throw for{0}!", players);
Console.WriteLine("Press space to throw a dart!");
Console.ReadKey();
throw1 = random_number.Next(1, 20);
Console.WriteLine("You got " + throw1 + " points!");
Console.WriteLine("\n second throw for{0}!", players);
Console.WriteLine("Press space to throw a dart!");
Console.ReadKey();
throw2 = random_number.Next(1, 20);
Console.WriteLine("You got " + throw2 + " points!");
Console.WriteLine("\n third throw for{0}!", players);
Console.WriteLine("Press space to throw a dart!");
Console.ReadKey();
throw3 = random_number.Next(1, 20);
Console.WriteLine("You got " + throw3 + " points!");
total_points = throw1 + throw2 + throw3;
Console.WriteLine("\nPoints for this round: " + total_points);
total_list.Add(total_points);
total_points = total_list.Sum(x => Convert.ToInt32(x));
players.Add_turn(throw1, throw2, throw3);
}
foreach (var players in player_list)
{
players.print_turns();
}
}
}
}
class Player
{
private string name;
private List<Turns> turn_list = new List<Turns>();
public Player(string _name)
{
name = _name;
}
public void Add_turn(int turn1, int turn2, int turn3)
{
turn_list.Add(new Turns(turn1, turn2, turn3));
}
public int calculate_points()
{
int total = 0;
foreach (var turns in turn_list)
{
total = total + turns.Get_Score();
}
return total;
}
public void print_turns()
{
Console.WriteLine("\n\n\n\n----------------------------------------");
Console.WriteLine("Points for {0}", name);
foreach (var turns in turn_list)
{
Console.WriteLine(turns);
}
Console.WriteLine("\n Total points: {0}", calculate_points());
}
public override string ToString()
{
return string.Format(" {0} ", name);
}
}
class Turns
{
private int turn1;
private int turn2;
private int turn3;
public Turns(int _turn1, int _turn2, int _turn3)
{
turn1 = _turn1;
turn2 = _turn2;
turn3 = _turn3;
}
public int Get_Score()
{
int totalt;
totalt = turn1 + turn2 + turn3;
return totalt;
}
public override string ToString()
{
return string.Format("\n throw 1: {0} \n throw 2: {1} \n throw 3: {2}", turn1, turn2, turn3);
}
}
类程序
{
公共静态void Main(字符串[]args)
{
游戏我的游戏=新游戏();
我的游戏;
}
}
公开课游戏
{
私有列表播放器_List=新列表();
私有列表总计_List=新列表();
公共游戏
{
随机数=新随机数();
int-throw1;
int-throw2;
int-throw3;
串更多玩家=“是”;
而(更多玩家==“是”)
{
Console.WriteLine(“玩家的名字是什么?:”);
player_list.Add(新播放器(Console.ReadLine());
控制台。WriteLine(“还有更多的玩家吗?”);
更多玩家=Console.ReadLine();
}
Console.WriteLine(“\n\n欢迎使用dartgame!\n”+
“\n游戏规则:每位玩家一次投掷3个飞镖。”+
“\n每一次投掷都可以得到0-20分。”+
“\n谁先得到301分,谁就是赢家!”);
Console.WriteLine(“\n层:”);
foreach(玩家列表中的var玩家)
{
控制台。WriteLine(播放器);
}
int总分=0;
而(!player_list.Any(x=>x.calculate_points()>=301))
{
foreach(玩家列表中的var玩家)
{
Console.WriteLine(“\n\n\n\n”);
Console.WriteLine(“\n对{0}的第一次投掷!”,玩家);
控制台。WriteLine(“按空格掷镖!”);
Console.ReadKey();
throw1=随机数。下一个(1,20);
Console.WriteLine(“你得到了”+throw1+“点数!”);
Console.WriteLine(“\n第二次掷{0}!”,玩家);
控制台。WriteLine(“按空格掷镖!”);
Console.ReadKey();
throw2=随机数。下一个(1,20);
控制台.WriteLine(“你得到了”+throw2+“点数!”);
Console.WriteLine(“\n第三次掷{0}!”,玩家);
控制台。WriteLine(“按空格掷镖!”);
Console.ReadKey();
throw3=随机数。下一个(1,20);
WriteLine(“你得到了”+throw3+“点数!”);
总分=throw1+throw2+throw3;
Console.WriteLine(“\n本轮积分:”+总积分);
总积分列表。添加(总积分);
total_points=total_list.Sum(x=>Convert.ToInt32(x));
玩家。增加回合(throw1,throw2,throw3);
}
foreach(玩家列表中的var玩家)
{
玩家。打印回合();
}
}
}
}
职业选手
{
私有字符串名称;
私有列表turn_List=新列表();
公共播放器(字符串_名称)
{
名称=_名称;
}
公共空间附加转弯(1号转弯、2号转弯、3号转弯)
{
匝道列表。添加(新匝道(匝道1、匝道2、匝道3));
}
公共整数计算_点()
{
int-total=0;
foreach(var轮次列表)
{
总数=总数+圈数。获得_分数();
}
返回总数;
}
公共作废打印(U)
{
Console.WriteLine(“\n\n\n\n--------------------------------------------------------”);
WriteLine(“点代表{0}”,名称);
foreach(var轮次列表)
{
控制台。写入线(圈);
}
WriteLine(“\n总分:{0}”,calculate_points());
}
公共重写字符串ToString()
{
返回string.Format(“{0}”,name);
}
}
班级轮换
{
私家车1号;
私人互联网2;
私人互联网3;
公共转弯(内转弯1、内转弯2、内转弯3)
{
turn1=_turn1;
turn2=_turn2;
第三回合=第三回合;
}
公共int获取_分数()
{
总积分;
totalt=匝道1+匝道2+匝道3;
返回totalt;
}
公共重写字符串ToString()
{
返回string.Format(“\n throw 1:{0}\n throw 2:{1}\n throw 3:{2}”,turn1,turn2,turn3);
}
}
好吧,这就是你检查是否有球员符合标准的方法:
player_list.Any(x => x.calculate_points() >= 301)
因此,只要符合标准,您就可以得到一名玩家:
player_list.Single(x => x.calculate_points() >= 301)
或所有匹配的玩家,如果有多个:
player_list.Where(x => x.calculate_points() >= 301)
或者只是第一个匹配的播放器,如果有多个但您只想要一个:
player_list.First(x => x.calculate_points() >= 301)
或者可能是得分最高的球员(不考虑平局分数):
好的,这就是你检查是否有球员符合标准的方式:
player_list.Any(x => x.calculate_points() >= 301)
因此,只要符合标准,您就可以得到一名玩家:
player_list.Single(x => x.calculate_points() >= 301)
或所有匹配的玩家,如果有多个:
player_list.Where(x => x.calculate_points() >= 301)
或者只是第一个匹配的播放器,如果有多个但您只想要一个:
player_list.First(x => x.calculate_points() >= 301)
或者可能是得分最高的球员(不考虑平局分数):
你试过什么?你被卡在哪一部分?只是注意在普通飞镖中,你不能超过目标分数。你试过什么?你被卡在哪一部分?只是注意,在普通飞镖中,你不能超过目标分数。回答得很好!希望看到不使用Linq的答案。这个问题似乎来自一个新的程序员,他们可能会更好地理解不使用Linq:)@TheMiddleMan:似乎是一个任意的请求,因为OP已经在使用Linq。谢谢,但是如果我想打印出获胜者,我怎么办,这是我无法理解的,比如