编写一个应用程序模拟抛硬币的过程。要求:用户输入1时,开始抛硬币,并显示结果,
输入2时,显示硬币每面出现的次数,以及出现的概率,
输入0时,结束应用程序,
要用C#

解决方案 »

  1.   

                Random ran = new Random();
                
                ran.Next(0, 1);
      

  2.   

     class Program
            {
                int neg = 0, pos = 0;
                int turn = 0;
                public Program(int turn)
                {
                    this.turn = turn;
                }            public void start()
                {
                    Random rand = new Random();
                    for (int i = 0; i < turn; i++)
                    {
                        result(rand.Next(0, 1));
                    }
                }
                public void result(int i)
                {
                    if (i == 0)                    neg++;//正面的次数
                    else if (i == 1)
                        pos++;//反面的次数
                    else
                        Console.WriteLine("error!");
                }
                public void print()
                {
                    Console.WriteLine("抛硬币的次数:{0}", turn);
                    Console.WriteLine("正面出现的次数:{0}", neg);
                    Console.WriteLine("正面出现的概率:{0}%", neg / turn * 100);
                    Console.WriteLine("反面出现的次数:{0}", pos);
                    Console.WriteLine("反面出现的概率:{0}%", pos / turn * 100);
                }            public void process()
                {
                    int j;
                    Console.WriteLine("请输入1开始抛硬币");
                    j = Convert.ToInt32(Console.ReadLine());
                    if (j == 1)
                    {
                        Console.WriteLine("请输入抛硬币的次数:{0}");
                        turn = Convert.ToInt32(Console.ReadLine());
                        start();
                        Console.WriteLine("输入数字2显示抛掷的结果:");
                        j = Convert.ToInt32(Console.ReadLine());
                    }
                    if (j == 2)                    print();
                        
                }
                static void Main(string[] args)
                {
                    Program play = new Program(0);
                    play.process();
                    Console.Read();            }
            }
      

  3.   


    谢谢上面各位楼主的帮忙,我的问题才得以解决,对6楼的答案,我调试了一下,做一下修改就完美啦!  再次感谢!
    class Program
        {
            int neg = 0, pos = 0;
            int turn = 0;
           
            public Program(int turn)
            {
                this.turn = turn;
            }        public void start()
            {
                Random rand = new Random();
                for (int i = 0; i < turn; i++)
                {
                    result(rand.Next(0, 2));
                }
            }
            public void result(int i)
            {
                if (i == 0)                neg++;//正面的次数 
                else if (i == 1)
                    pos++;//反面的次数 
                else
                    Console.WriteLine("error!");
            }
            public void print()
            {
                Console.WriteLine("抛硬币的次数:{0}", turn);
                Console.WriteLine("正面出现的次数:{0}", neg);
                Console.WriteLine("正面出现的概率:{0}%", neg * 100 / turn);
                Console.WriteLine("反面出现的次数:{0}", pos);
                Console.WriteLine("反面出现的概率:{0}%", pos * 100 / turn );
            }        public void process()
            {
                int j;
                Console.WriteLine("请输入1开始抛硬币");
                j = Convert.ToInt32(Console.ReadLine());
                if (j == 1)
                {
                    Console.WriteLine("请输入抛硬币的次数:");
                    turn = Convert.ToInt32(Console.ReadLine());
                    start();
                    Console.WriteLine("输入数字2显示抛掷的结果:");
                    j = Convert.ToInt32(Console.ReadLine());
                }
                if (j == 2)                print();        }
            static void Main(string[] args)
            {
                Program play = new Program(0);
                play.process();
                Console.Read();        }
        }