# C＃骰子游戏，总和返回最后2卷的总和不是整个游戏 -- c# 领域 和 visual-studio-2010 领域 和 visual-studio 领域 和 sum 领域 和 average 领域 相关 的问题

## C# Dice game, Sum returns only sum of last 2 rolls not whole game

0

### 问题

` `         Console.BufferHeight = 500;         Random x = new Random();         int throw_times = 1;          int[] dice = new int[2];         dice[0] = x.Next(1, 7);         dice[1] = x.Next(1, 7);          for (int i = 1; i <= 100; i++)         {             dice[0] = x.Next(1, 7);             dice[1] = x.Next(1, 7);              int total_var = dice[0] + dice[1];             int[] total_array = {dice[0] + dice[1]};//total in array              Console.Write("Throw " + throw_times + ": " + dice[0] + " and " + dice[1] + " = ");             Console.WriteLine(total_var);             throw_times++;              Array.Sort(dice);              for (int a = dice.Length - 1; a >= 0; a--)             {                 int s = dice[a];                 Console.WriteLine("#" + s);             }         }          Console.WriteLine("Total sum: " + dice.Sum());//only returns sum of last 2 rolls         Console.WriteLine("Average: " + dice.Average());//only return average of last 2 rolls  ` `

## 如果有人知道我如何获得总卷的总和，请回答，非常赞赏。

I'm creating a dice game with 2 dice, i have got it working but only problem is i cant seam to get the Sum from all the rolls(whole game), i only get the sum of last 2 rolls if i use the Sum method, how do i find the Sum of all the rolls? Here's my code:

``        Console.BufferHeight = 500;         Random x = new Random();         int throw_times = 1;          int[] dice = new int[2];         dice[0] = x.Next(1, 7);         dice[1] = x.Next(1, 7);          for (int i = 1; i <= 100; i++)         {             dice[0] = x.Next(1, 7);             dice[1] = x.Next(1, 7);              int total_var = dice[0] + dice[1];             int[] total_array = {dice[0] + dice[1]};//total in array              Console.Write("Throw " + throw_times + ": " + dice[0] + " and " + dice[1] + " = ");             Console.WriteLine(total_var);             throw_times++;              Array.Sort(dice);              for (int a = dice.Length - 1; a >= 0; a--)             {                 int s = dice[a];                 Console.WriteLine("#" + s);             }         }          Console.WriteLine("Total sum: " + dice.Sum());//only returns sum of last 2 rolls         Console.WriteLine("Average: " + dice.Average());//only return average of last 2 rolls ``

## 回答列表

1

` Sum `` Average ` Average 扩展方法将计算阵列中当前的内容的总和和平均值，因为您正在重新设置每次试用，它只包含最后两个项目。

` ` int[] dice = new int[2]; int sum = 0;  for (int i = 1; i <= 100; i++) {     dice[0] = x.Next(1, 7);     dice[1] = x.Next(1, 7);     sum += dice[0] + dice[1];      ... }  Console.WriteLine("Total sum: " + sum);  ` `

` ` Console.WriteLine("Total sum: " + (sum / 200)); // 200 because you roll 2 dice in 100 trials  ` `

The `Sum` and `Average` extension methods will compute the sum and average of what's currently in the array, and because you're re-setting the array with every trial, it will only ever contain the last two items.

To get the sum or average of all the dice rolls, you'd either have to expand your array so that it records all the dice rolls made throughout your program, or just keep a running total (which seems a lot simpler):

``int[] dice = new int[2]; int sum = 0;  for (int i = 1; i <= 100; i++) {     dice[0] = x.Next(1, 7);     dice[1] = x.Next(1, 7);     sum += dice[0] + dice[1];      ... }  Console.WriteLine("Total sum: " + sum); ``

Now it's not entirely clear from your question if you also want the average of all the dice rolls, or just the last two, but since you know the sum and you know the total (because that's hard coded in your app), you can compute the average of all dice rolls pretty easily:

``Console.WriteLine("Total sum: " + (sum / 200)); // 200 because you roll 2 dice in 100 trials ``

## 相关问题

4  没有考虑零值的Numpy的许多阵列的平均值  ( Average of a number of arrays with numpy without considering zero values )

5  计算STL映射脱编子范围的平均值的有效方法  ( Efficient way to calculate average value over disjoint subranges of stl map )

0  使用r计算数据集中每个独特动物的特性平均值  ( Calculating the trait average of each unique animal in a data set using r )

-2  选择数月计数的SELECT语句  ( Select statement with count for months )

1  如何在Super Store Data-Tableau从订单和发货日期之间找到国家方面的平均运输时间  ( How to find country wise average shipping time between order and shipping date f )

0  sql avg subselect  ( Sql avg in subselect )

1  从许多文本文件中读取数字和平均值它们python  ( Read numbers from many text files and average them python )

3  MATLAB：平均时间序列数据没有循环？  ( Matlab averaging time series data without loops )

3  Unity3D - 查找每秒更改的浮点的平均值  ( Unity3d find average value of float that changes each second )

0  将数字用户类型的平均值设置为数组  ( Finding average of the numbers users type into an array )

0  使用Select AVG具有参数“选择AVG（@Parameter）”SQL！  ( Use select avg with parameters select avgparameter sql )

2  我如何每小时获得平均水平，排除10分钟的分钟  ( How can i get average per hour excluding 10 minute differences )

2  通过ISQL中的ISQL中的平均值在列中减去列中的值  ( Subtracting a value in a column by average of the same column without using a va )

0  计算平均值，而不是数组中的最高值和最低值  ( Calculating average without highest and lowest values in an array )

0  除了Quicksight的当前几周外，计算最后4周平均值？  ( Calculate last 4 weeks average except current week in quicksight )

1  计算不规则数据的移动平均值  ( Calculating moving average over irregular data )

0  平均日常数据从一周的数据  ( Mean day with hourly data from a week )

0  如何在MySQL中汇总桌面以获得基于几个列的3个月收入平均值？  ( How can i summarize in mysql a table to obtain a 3 month revenue average based o )

1  将拆分文件的MD5结果与整体的MD5进行比较  ( Comparing the md5 results of split files against the md5 of the whole )

4  如何创建计算EPPlus中的值的数据字段？  ( How can i create a datafield that calculates values in epplus )

-2  如何在SQL中获得平均账单？  ( How do i get average if statement in sql )

-3  Java中数字的总和和平均值  ( Sum and average of numbers in java )

0  互机和C ++的平均值  ( Interator and average in c )

2  lisp - 如何获得嵌套列表的平均长度？  ( Lisp how to get the average length for nested lists )

0  返回该部分中所有学生的平均标记  ( Return the average mark for all student in that section )