c# - 在列表中跟踪分数以及姓名

标签 c# list loops

亲爱的StackOverflow,您好!我是 C# 新手,我一直坚持“添加分数”计划。

我想做的基本想法是:

  • 输入玩家人数
  • 说出上述玩家人数
  • 让所有玩家三轮加 3 分
  • 显示每个玩家每轮的得分。

例如:

players = 1
name = bilbo

round1 begins:
score1 = 10
score2 = 10
score3 = 10

round2 begins:
score1 = 20
score2 = 20
score3 = 20

round2 begins:
score1 = 30
score2 = 30
score3 = 30

game ends:
Congratulations bilbo your score was as follows:
round1:
10,10,10
round2:
20,20,20
round3:
30,30,30
Total score: 180

我的问题是我不知道如何跟踪每一轮。现在它对我的工作方式是它会覆盖分数,因此显示的唯一分数是最后一个分数。在这种情况下,输出将类似于

total score: 90 // aka the last 3 scores

如何跟踪每一轮并将该分数与正确的玩家联系起来?

我知道验证很少甚至没有,但这对我来说不是问题:)

祝大家周末愉快,提前感谢您提供的任何帮助。

到目前为止的代码:

class Program
{
  static void Main(string[] args)
  {
    List<Player> players = new List<Player>();
    Console.WriteLine("How many players?");
    int howManyPlayers = Convert.ToInt32(Console.ReadLine());

    for (int i = 0; i < howManyPlayers; i++)
    {
      Console.WriteLine("Name the player");
      Player playa = new Player() { Name = Console.ReadLine()};
      players.Add(playa);
    }

    for (int i = 0; i < 3; i++)
    {
      foreach (var item in players)
      {
        Console.WriteLine("Enter points1 for round {}", i);
        int round1Input = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter points2 for round {}", i);
        int runda2Input = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter points3 for round {}", i);
        int runda3Input = Convert.ToInt32(Console.ReadLine());
        item.totalScore(item.arrow1 = round1Input, 
        item.arrow2 = runda2Input,   item.arrow3 = runda3Input);
      }
    }

    printAllPlayers(players);
  }

  static void printAllPlayers(List<Player> players)
  {
    Console.WriteLine("Printing {0} players ...", players.Count);
    foreach (Player player in players)
    {
      Console.WriteLine("Player: {0} \nScore: {1}", player.Name, player.Score);
    }
  }
}

class Player
{
  public string Name { get; set; }
  public int Score { get; set; }
  public int arrow1 { get; set; }
  public int arrow2 { get; set; }
  public int arrow3 { get; set; }

  public void totalScore(int arrow1, int arrow2, int arrow3)
  {
    Score = arrow1 + arrow2 + arrow3;
  }
}

最佳答案

根据 John W 的建议:

    static void Main(string[] args)
    {

        List<Player> players = new List<Player>();
        Console.WriteLine("How many players?");
        int howManyPlayers = Convert.ToInt32(Console.ReadLine());

        for (int i = 0; i < howManyPlayers; i++)
        {
            Console.WriteLine("Name the player");
            Player playa = new Player() { Name = Console.ReadLine() };
            players.Add(playa);
        }


        for (int i = 0; i < 3; i++)
        {
            foreach (var item in players)
            {
                // to avoid this repeating code, you could create a Round class
                // populate the Arrow*x* properties in a loop
                // and add the populated Round to the Player.
                Console.WriteLine("Player {0} - Enter points 1 for round {1}", item.Name, i + 1);
                int round1Input = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Player {0} - Enter points 2 for round {1}", item.Name, i + 1);
                int runda2Input = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Player {0} - Enter points 3 for round {1}", item.Name, i + 1);
                int runda3Input = Convert.ToInt32(Console.ReadLine());
                item.Rounds.Add(new Round(round1Input, runda2Input, runda3Input));
            }
        }

        printAllPlayers(players);
    }


    static void printAllPlayers(List<Player> players)
    {
        Console.WriteLine("Printing {0} players ...", players.Count);
        foreach (Player player in players)
        {
            Console.WriteLine("Player: {0} \nScore: {1}", player.Name, player.TotalScore);
        }
        Console.ReadLine();
    }

}

class Round
{
    public Round()
    {
    }

    public Round(int Arrow1, int Arrow2, int Arrow3)
    {
        arrow1 = Arrow1;
        arrow2 = Arrow2;
        arrow3 = Arrow3;
    }

    public int arrow1 { get; set; }
    public int arrow2 { get; set; }
    public int arrow3 { get; set; }
    public int Score
    {
        get
        {
            return arrow1 + arrow2 + arrow3;
        }

    }
}

class Player
{
    public Player()
    {
        Rounds = new List<Round>();
    }

    public string Name { get; set; }
    public List<Round> Rounds { get; set; }

    public int TotalScore
    {
        get
        {
            int score;
            score = 0;
            // iterate through the player's Rounds, summing the Scores
            foreach (var r in Rounds)
            {
                score += r.Score;
            }
            return score;
        }
    }

关于c# - 在列表中跟踪分数以及姓名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21781230/

相关文章:

c# - C# 或 JIT 编译器是否足够智能以处理此问题?

c# - 如何使用 ITextSharp 验证 pdf 是否基于文本?

r - 将列表列表转换为 data.frame 或 tibble (R) 的单个嵌套行

c# - 使用 LINQ 关联两个列表之间的值?

c - OpenMP 令人尴尬的并行循环,没有加速

arrays - 如何使用 perl 从第一次出现具有特定值的元素开始遍历数组?

c - 简单的 do while 循环使用 while(true);

c# - 带有 MVVMCross 6.2+ 的选项卡

c# - 如何回溯函数调用?

javascript - 通过另一个下拉列表 Id ASP.Net 过滤下拉列表