1 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
制作一個(gè)數(shù)組int[] counts = new int[13]并使用counts[total]++;; 最后,循環(huán)它:
for(int i = 2 ; i <= 12 ; i++)
// etc
(注意:您可以使用 11 個(gè)項(xiàng)目的數(shù)組并不斷處理兩個(gè),但是......這可能不值得)
就像是:
static void Main()
{
Console.WriteLine("Investigation 1");
Console.WriteLine("===============");
Console.WriteLine();
Console.Write("How many sets of tests? 1 to 10: ");
int sets = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
Random r = new Random();
int[] counts = new int[13];
for (int ctr = 0; ctr < 36 * sets; ctr++)
{
int a = r.Next(1, 7), b = r.Next(1, 7), total = a + b;
Console.WriteLine($"Roll {(ctr + 1)}: {a} + = {total}");
counts[total]++;
}
Console.WriteLine("=======================");
Console.WriteLine();
Console.WriteLine("Total Expected Actual");
Console.WriteLine("===== ======== ======");
for(int i = 2; i <= 12; i++)
{
var expected = sets * (6 - Math.Abs(7 - i));
Console.WriteLine($" {i} {expected} {counts[i]}");
}
}
對(duì)于直方圖:
var maxCount = counts.Max(); // needs "using System.Linq;" at the top
for (int i = 2; i <= 12; i++)
{
var width = ((Console.WindowWidth - 10) * counts[i]) / maxCount; // make it proportional
Console.WriteLine($"{i}\t{new string('*', width)}");
}
- 1 回答
- 0 關(guān)注
- 143 瀏覽
添加回答
舉報(bào)