• 投掷6000次 16 出现的次数(已有3种解法)


    第一种:

    Console.WriteLine("投掷100次的实验:"); //提示信息
    Random randomNum = new Random(); //创建一个随机数
    int num1 = 0; //定义出现1的次数
    int num2 = 0; //定义出现2的次数
    int num3 = 0; //定义出现3的次数
    int num4 = 0; //定义出现4的次数
    int num5 = 0; //定义出现5的次数
    int num6 = 0; //定义出现6的次数


    for (int i = 1; i <= 100; i++) //for语句循环
    {
    int num = randomNum.Next(1, 7); //定义一个变量接受随机数

    switch (num)
    {
    case 1:
    {
    num1++;
    break;
    }

    case 2:
    {
    num2++;
    break;
    }
    case 3:
    {
    num3++;
    break;
    }
    case 4:
    {
    num4++;
    break;
    }

    case 5:
    {
    num5++;
    break;
    }

    case 6:
    {
    num6++;
    break;

    }
    default:
    break;

    }

    Console.WriteLine("第{0}次的数是:{1}", i, num);
    }

    Console.WriteLine("1出现过{0}次", num1);
    Console.WriteLine("2出现过{0}次", num2);
    Console.WriteLine("3出现过{0}次", num3);
    Console.WriteLine("4出现过{0}次", num4);
    Console.WriteLine("5出现过{0}次", num5);
    Console.WriteLine("6出现过{0}次", num6);

    //验证是否是100次
    int sum = num1 + num2 + num3 + num4 + num5 + num6;
    Console.WriteLine(sum);

    第二种:

    namespace _2011._12._3
    {
    class Program
    {
    static void Main(string[] args)
    {
    int[] count = new int[7] ;
    Random randomNum = new Random();
    int num = 0;
    for(int i = 0 ;i <100;i++)
    {
    num = randomNum.Next(1,7);
    count[num] ++ ;
    }

    Console.WriteLine("1出现的次数是:{0}",count[1]);

    Console.WriteLine("2出现的次数是:{0}", count[2]);

    Console.WriteLine("3出现的次数是:{0}", count[3]);

    Console.WriteLine("4出现的次数是:{0}", count[4]);

    Console.WriteLine("5出现的次数是:{0}", count[5]);

    Console.WriteLine("6出现的次数是:{0}", count[6]);

    Console.WriteLine("一共投掷了{0}次!",count[1]+count[2]+count[3]+count[4]+count[5]+count[6]);
    }
    }
    }

    第三种:

    class Program
    {
    static void Main(string[] args)
    {
    //计算投掷600次1-6出现的次数
    int[] Array = new int[7];//array[1]代表face=1
    Random rannum=new Random();

    for (int counter = 0; counter <= 6000; counter++)
    {
    ++Array[rannum.Next(1, 7)];//默认的array[]=0,所以当array[1]出现1次就+1;
    }

    for (int face = 1; face <= 6; face++)
    {
    Console.WriteLine("face {0} is {1} ",face,Array[face]);//输出array[]的值,就是出现的次数
    }
    }


    }





  • 相关阅读:
    Accept Xcode/iOS License to run git
    Public Key Retrieval is not allowed
    No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer
    HttpURLConnection调用Restful接口
    关于线程同步(7种同步方式)
    面试(初级)
    面试题(高级)
    RedisTemplate和StringRedisTemplate的区别 RedisTemplate几种序列化方式比较
    Redis 3.2.1集群 —— Redis-trib.rb工具详解(含原理)
    Redis 3.2.1集群 —— CLUSTER MEET ip port
  • 原文地址:https://www.cnblogs.com/IAmBetter/p/2288772.html
Copyright © 2020-2023  润新知