A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 一片白 中级黑马   /  2014-4-28 10:47  /  1144 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 一片白 于 2014-4-28 11:35 编辑

编写一程序,用1~6之间的随机数来模拟掷骰子游戏,统计扔100次后各点数出现的次数并输出。

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

2 个回复

倒序浏览
手头木有VS 记事本写的 不知道有没有写错。。
  1. int[] count={0,0,0,0,0,0};//存储每个点数的掷出次数
  2. Random r=new Random();
  3. for(int i=0;i<100;i++)
  4. {
  5. int result=r.Next(1,7);//用来生成随机数
  6. switch(result)//判断随机数的值 并且统计次数
  7. {case 1;count[0]++;break;
  8. case 2;count[1]++;break;
  9. case 3;count[2]++;break;
  10. case 4;count[3]++;break;
  11. case 5;count[4]++;break;
  12. case 6;count[5]++;break;
  13. }
  14. }
  15. Console.WriteLine("1出现{0}次,2出现{1}次,3出现{2}次,4出现{3}次,5出现{4}次,6出现{5}次",count[0],count[1],count[2],count[3],count[4],count[5]);
复制代码

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
我对上面那个仁兄的代码优化了一下。。。。。。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;

  5. namespace test
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Random ra = new Random();
  12.             int[] count={0,0,0,0,0,0};
  13.             int temp = 0;
  14.             for (int i = 1; i <= 100; i++)
  15.             {
  16.                 temp = ra.Next(1, 7);
  17.                 count[--temp]++;
  18.             }
  19.             Console.WriteLine("1出现{0}次,2出现{1}次,3出现{2}次,4出现{3}次,5出现{4}次,6出现{5}次", count[0], count[1], count[2], count[3], count[4], count[5]);
  20.             Console.Read();
  21.         }
  22.     }
  23. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马