简体   繁体   English

如何将运行总数/计数器添加到 C# 控制台应用程序

[英]How to add running total/ counter to C# console application

My application is taking user input (bowling scores) and averages them out.我的应用程序正在获取用户输入(保龄球分数)并将它们求平均值。 The issue I am coming to is the requirement of allowing the user to stop at any time.我要解决的问题是允许用户随时停止的要求。 I placed 10 int variables and assumed one will not be calculating over 10. My question, is how do I keep a running total/ tally, so that if the user only inputs 2 scores to average, that I only divide by 2?我放置了 10 个 int 变量,并假设一个不会超过 10。我的问题是,我如何保持运行总数/计数,以便如果用户只输入 2 个分数来求平均值,我只除以 2? Thanks for any help.谢谢你的帮助。

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();

This is how I coded the program.这就是我编写程序的方式。 Do I create a variable, and assign itself inside of a while statement, to declare if any other buttons are pressed (program continues to another user input for game 2), to add 1 to the variable?我是否创建了一个变量,并在 while 语句中赋值,以声明是否按下了任何其他按钮(程序继续到游戏 2 的另一个用户输入),将 1 添加到变量中?

Use a List<int> collection to store the scores, then either使用List<int>集合来存储分数,然后

1) Divide the sum total of the values in the list and divide by the list's count property: 1) 将列表中值的总和除以列表的 count 属性:

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

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

var average = total / scores.Count;

2) Use LINQ's .Average() method to quickly get the average of all the values in the list: 2)使用LINQ的.Average()方法快速获取列表中所有值的平均值:

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

Naturally use any data types or conversions needed to obtain the level of accuracy you need/desire.自然地使用所需的任何数据类型或转换来获得您需要/期望的准确度级别。

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();
}

the count variable will keep a running total on the number of inputs and the game1 variable will continue to add the scores up. count 变量将保持输入数量的运行总数,而 game1 变量将继续增加分数。

UPDATE for question asked;更新问题;

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;

You can push into a list and use Sum() and Average() extensions.您可以推入列表并使用 Sum() 和 Average() 扩展。 TO keep aceepting scores, you can use while loop要保持获得分数,您可以使用 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM