3、(投骰子)编写一个程序,模拟投两个骰子。用Random类对象投第一个骰子,并再次用Random投第二个骰子
,然后计算两个值的和。说明:由于每个骰子显示1~6的整数值,因此两个骰子的和为2~12,7最常见,2和12最不常见
。程序将投两个骰子36000次,用一维数组估算每个和出现的次数,用表格形式显示结果。并确定和是否合理,即有6种方式投出7
,因此有六分之一的可能投出7。
源代码:(经过校验都是能够正确运行的。):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//按照题目要求投出2的概率是:36分之一,投出3的概率是36分之二,投出4的概率是36分之三,投出5的概率是36分之4,投出6的概率是36分之6,
//投出7的概率是36分之五,投出8的概率是36分之五,投出9的概率是36分之4,投出10的概率是36分之三,投出11的概率是二,投出12的概率是36分之一
namespace Employee
{
class Class2
{
//方法封装
public void printResult(string[] info,int[] sum)
{
for (int j = 0; j < 11; j++)
{
Console.Write(info[j]);
Console.Write(" ");
if (j == 10)
{
Console.WriteLine("");
}
}
Console.Write("其统计的出现次数为:");
for (int k = 0; k < 11; k++)
{
Console.Write(sum[k]);
Console.Write(" ");
if (k == 10)
{
Console.WriteLine("");
}
}
}
public void Result(double count1_1,double count1_2,double count1_3,double count1_4,
double count1_5,double count1_6,double count1_7,double count1_8,double count1_9,double count1_10,double count1_11)
{
double result;
result=count1_1/36000;
Console.WriteLine("2出现的概率为:"+result);
if (result != (1.0 / 36))
{
Console.WriteLine("该数值不合法");
}
else
{
Console.WriteLine("该数值经过计算合法");
}
result = count1_2 / 36000;
Console.WriteLine("3出现的概率为:" + result);
if (result != (1.0 / 36))
{
Console.WriteLine("该数值不合法");
}
else
{
Console.WriteLine("该数值经过计算合法");
}
result = count1_3 / 36000;
Console.WriteLine("4出现的概率为:" + result);
if (result != (1.0 / 36))
{
Console.WriteLine("该数值不合法");
}
else
{
Console.WriteLine("该数值经过计算合法");
}
result = count1_4 / 36000;
Console.WriteLine("5出现的概率为:" + result);
if (result != (1.0 / 36))
{
Console.WriteLine("该数值不合法");
}
else
{
Console.WriteLine("该数值经过计算合法");
}
result = count1_5 / 36000;
Console.WriteLine("6出现的概率为:" + result);
if (result != (1.0 / 36))
{
Console.WriteLine("该数值不合法");
}
else
{
Console.WriteLine("该数值经过计算合法");
}
result = count1_6 / 36000;
Console.WriteLine("7出现的概率为:" + result);
if (result != (1.0 / 36))
{
Console.WriteLine("该数值不合法");
}
else
{
Console.WriteLine("该数值经过计算合法");
}
result = count1_7 / 36000;
Console.WriteLine("8出现的概率为:" + result);
if (result != (1.0 / 36))
{
Console.WriteLine("该数值不合法");
}
else
{
Console.WriteLine("该数值经过计算合法");
}
result = count1_8 / 36000;
Console.WriteLine("9出现的概率为:" + result);
if (result != (1.0 / 36))
{
Console.WriteLine("该数值不合法");
}
else
{
Console.WriteLine("该数值经过计算合法");
}
result = count1_9 / 36000;
Console.WriteLine("10出现的概率为:" + result);
if (result != (1.0 / 36))
{
Console.WriteLine("该数值不合法");
}
else
{
Console.WriteLine("该数值经过计算合法");
}
result = count1_10 / 36000;
Console.WriteLine("11出现的概率为:" + result);
if (result != (1.0 / 36))
{
Console.WriteLine("该数值不合法");
}
else
{
Console.WriteLine("该数值经过计算合法");
}
result = count1_11 / 36000;
Console.WriteLine("12出现的概率为:" + result);
if (result != (1.0 / 36))
{
Console.WriteLine("该数值不合法");
}
else
{
Console.WriteLine("该数值经过计算合法");
}
}
static void Main(string[] args)
{
Class2 re = new Class2();
Random ran = new Random();
int[] num1 = new int[36000];
int[] num2 = new int[36000];
//define two varible to statisticsnumber
string[] info = new string[11] { "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };
int item;
int count1,count2,count3,count4,count5,count6,count7,count8,count9,count10,count11;
count1 = 0;
count2 = 0;
count3 = 0;
count4 = 0;
count5 = 0;
count6 = 0;
count7 = 0;
count8 = 0;
count9 = 0;
count10 = 0;
count11 = 0;
//难点之处要去除重复出现的数字。但是现实情况中时间发生的次数统计并不用考虑这些,拿实际值与理论作对比。
for (int i = 0; i < 36000; i++)
{
num1[i] = ran.Next(1, 7);
num2[i] = ran.Next(1, 7);
item = num1[i] + num2[i];
if (item==2)
{
count1++;
}
else if(item==3)
{
count2++;
}else if(item==4)
{
count3++;
}
else if (item == 5)
{
count4++;
}
else if (item == 6)
{
count5++;
}
else if (item == 7)
{
count6++;
}
else if (item == 8)
{
count7++;
}
else if (item == 9)
{
count8++;
}
else if (item == 10)
{
count9++;
}
else if (item == 11)
{
count10++;
}
else if (item == 12)
{
count11++;
}
}
int[] sum = new int[11] { count1, count2, count3, count4, count5, count6, count7, count8, count9, count10, count11 };
Console.Write("骰子可能出现的和为:");
re.printResult(info,sum);
//按照题目要求投出2的概率是:36分之一,投出3的概率是36分之二,投出4的概率是36分之三,
//投出5的概率是36分之4,投出6的概率是36分之6,
//投出7的概率是36分之五,投出8的概率是36分之五,投出9的概率是36分之4,
//投出10的概率是36分之三,投出11的概率是二,投出12的概率是36分之一
//强制类型转换
double count1_1 = (double)count1;
double count1_2 = (double)count2;
double count1_3 = (double)count3;
double count1_4 = (double)count4;
double count1_5 = (double)count5;
double count1_6 = (double)count6;
double count1_7 = (double)count7;
double count1_8 = (double)count8;
double count1_9 = (double)count9;
double count1_10 = (double)count10;
double count1_11 = (double)count11;
re.Result(count1_1, count1_2, count1_3, count1_4, count1_5, count1_6, count1_7, count1_8, count1_9, count1_10, count1_11);
}
}
}
代码可能有些拖沓,只是还没有改进,如果改进了就会及时补充的。
今天还会有一个梢会补充,