用for写的:- int[] val ={ 0, 0, 0, 0, 0, 0 };
- Random rval = new Random();
- for (int i = 1; i <= 100; i++)
- {
- if (rval.Next(1, 7) == 1) val[0] += 1;
- if (rval.Next(1, 7) == 2) val[1] += 1;
- if (rval.Next(1, 7) == 3) val[2] += 1;
- if (rval.Next(1, 7) == 4) val[3] += 1;
- if (rval.Next(1, 7) == 5) val[4] += 1;
- if (rval.Next(1, 7) == 6) val[5] += 1;
- }
- Console.WriteLine("The number 1 occures {0} times. ", val[0]);
- Console.WriteLine("The number 2 occures {0} times. ", val[1]);
- Console.WriteLine("The number 3 occures {0} times. ", val[2]);
- Console.WriteLine("The number 4 occures {0} times. ", val[3]);
- Console.WriteLine("The number 5 occures {0} times. ", val[4]);
- Console.WriteLine("The number 6 occures {0} times. ", val[5]);
- Console.ReadKey();
- }
- }
复制代码 switch写的 自己看看- //六个变量分别记录各个数出现的次数。
- int Count_1 = 0, Count_2 = 0, Count_3 = 0, Count_4 = 0, Count_5 = 0, Count_6 = 0;
- Random ran = new Random((int)DateTime.Now.Ticks);
- for (int i = 0; i<600;i++)
- {
-
- switch (ran.Next(1, 7))
- {
- case 1: Count_1++;
- break;
- case 2: Count_2++;
- break;
- case 3: Count_3++;
- break;
- case 4: Count_4++;
- break;
- case 5: Count_5++;
- break;
- case 6: Count_6++;
- break;
- }
- }
- StringBuilder sb = new StringBuilder();
- sb.AppendLine("1出现了"+Count_1.ToString()+"次");
- sb.AppendLine("2出现了" + Count_2.ToString() + "次");
- sb.AppendLine("3出现了" + Count_3.ToString() + "次");
- sb.AppendLine("4出现了" + Count_4.ToString() + "次");
- sb.AppendLine("5出现了" + Count_5.ToString() + "次");
- sb.AppendLine("6出现了" + Count_6.ToString() + "次");
- Response.Write(sb.ToString());
复制代码 |