【问题标题】:How to add running total/ counter to C# console application如何将运行总计/计数器添加到 C# 控制台应用程序
【发布时间】:2017-10-17 20:32:06
【问题描述】:

我的应用程序正在接受用户输入(保龄球得分)并将它们平均化。我要解决的问题是允许用户随时停止的要求。我放置了 10 个 int 变量,并假设一个变量不会超过 10。我的问题是,我如何保持运行总计/计数,以便如果用户只输入 2 个分数来平均,我只除以 2?感谢您的帮助。

Console.WriteLine("Please input the score of game 1: ");
g1 = ReadLine();
game1 = int.Parse(g1);
Console.WriteLine("Would you like to add more scores? Press 'n' to continue to averaging, press any other key to continue!");
playagain = ReadLine();
if(playagain =="n")
{
    calculating(); 
}
Console.Clear();

这就是我编写程序的方式。我是否要创建一个变量,并在 while 语句中赋值,以声明是否按下了任何其他按钮(程序继续到游戏 2 的另一个用户输入),将 1 添加到变量?

【问题讨论】:

  • 您可以使用List,然后除以Count
  • 使用集合 (List<int>) 保留任意数量的输入

标签: c# visual-studio oop object


【解决方案1】:

使用List<int> 集合来存储分数,然后

1) 将列表中值的总和除以列表的计数属性:

var scores = new List<int>();
int total = 0;

foreach(var score in scores)
{
  total += score;
}

var average = total / scores.Count;

2)使用LINQ的.Average()方法快速获取列表中所有值的平均值:

var scores = new List<int>();
var average = scores.Average();

自然地使用任何数据类型或转换来获得您需要/期望的准确度。

【讨论】:

  • 使用LINQ更干净
  • @Kokombads 绝对是,但不是每个人都知道如何使用它,所以我提供了替代方案。
【解决方案2】:

您可以推入列表并使用 Sum() 和 Average() 扩展。为了保持分数,你可以使用 while 循环

    Console.WriteLine("Please input the score of game 1: ");
    List<int> scores = new List<int>();
    int game1 = int.Parse(Console.ReadLine());
    scores.Add(game1);
    Console.WriteLine("Would you like to add more scores? Press 'n' to continue to averaging, press any other key to continue!");
    string playagain = Console.ReadLine();
    while(playagain != "n")
    {
        Console.WriteLine("Please input the score of next game: ");
        scores.Add(int.Parse(Console.ReadLine()));
        Console.WriteLine("Would you like to add more scores? Press 'n' to continue to averaging, press any other key to continue!");
        playagain = Console.ReadLine();
    }
    Console.WriteLine("Total is " + scores.Sum());
    Console.WriteLine("Average is " + scores.Average());
    Console.Clear();

【讨论】:

    【解决方案3】:
    int count = 0; 
    while(playagain != "n")
    {
        Console.WriteLine("Please input the score of game 1: ");
        g1 = ReadLine();
        game1 += int.Parse(g1);
        count++;
        Console.WriteLine("Would you like to add more scores? Press 'n' to continue to averaging, press any other key to continue!");
        playagain = ReadLine();        
        Console.Clear();
    }
    

    count 变量将保持输入数量的运行总和,而 game1 变量将继续将分数相加。

    更新问题;

    class OtherClass{
       public int Counter {set; get;}
      .
      .//other logic
      .
    }
    OtherClass otherClass = new OtherClass();
    int count = 0; 
    while(playagain != "n")
    {
        Console.WriteLine("Please input the score of game 1: ");
        g1 = ReadLine();
        game1 += int.Parse(g1);
        count++;
        Console.WriteLine("Would you like to add more scores? Press 'n' to continue to averaging, press any other key to continue!");
        playagain = ReadLine();        
        Console.Clear();
    }
    otherClass.Counter = count;
    

    【讨论】:

    • 我曾经使用过这种方法,但希望在另一个类中调用我的所有变量和计数器来计算总数。我的问题是另一个班级将计数器值视为 0,因为它是在班级之前分配的。有什么办法解决这个问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多