本帖最后由 盘晟 于 2013-6-18 21:35 编辑
第一种代码:
int[] values = { 0, 0, 0, 0, 0, 0 };
Random r = new Random();
int number = r.Next(1,7);
for (int i = 0; i < 100; i++)
{
if (number == 1)
values[0]++;
if (number == 2)
values[1]++;
if (number == 3)
values[2]++;
if (number == 4)
values[3]++;
if (number == 5)
values[4]++;
if (number == 6)
values[5]++;
}
Console.WriteLine("1点出现的次数为{0}",values[0]);
Console.WriteLine("2点出现的次数为{0}", values[1]);
Console.WriteLine("3点出现的次数为{0}", values[2]);
Console.WriteLine("4点出现的次数为{0}", values[3]);
Console.WriteLine("5点出现的次数为{0}", values[4]);
Console.WriteLine("6点出现的次数为{0}", values[5]);
输出结果:
第二种代码:
int[] values = { 0, 0, 0, 0, 0, 0 };
Random r = new Random();
for (int i = 0; i < 100; i++)
{
if (r.Next(1,7) == 1)
values[0]++;
if (r.Next(1,7) == 2)
values[1]++;
if (r.Next(1, 7) == 3)
values[2]++;
if (r.Next(1, 7) == 4)
values[3]++;
if (r.Next(1, 7) == 5)
values[4]++;
if (r.Next(1, 7) == 6)
values[5]++;
}
Console.WriteLine("1点出现的次数为{0}",values[0]);
Console.WriteLine("2点出现的次数为{0}", values[1]);
Console.WriteLine("3点出现的次数为{0}", values[2]);
Console.WriteLine("4点出现的次数为{0}", values[3]);
Console.WriteLine("5点出现的次数为{0}", values[4]);
Console.WriteLine("6点出现的次数为{0}", values[5]);
Console.ReadKey();
输出结果:
如果定义int number=r.next(1,7);结果不对,直接在循环里r.next(1,7)结果则是对的?
|