小编Rob*_*idt的帖子

随机数生成循环生成相同的数字

我还是C#的新手,我正在尝试构建一个简短的程序,该程序模拟将硬币掷X次的次数:

        // Declarations
        int headCount = 0;
        int tailCount = 0;

        // Main Program Logic
        Console.WriteLine("Press enter to begin");
        Console.ReadLine();
        for (int x = 1; x <= 25; x++)
        {
            Random rnd = new Random();
            int flip = rnd.Next(1, 3);
            if (flip == 1)
            {
                Console.WriteLine("Heads");
                headCount++;
            }
            else
            {
                Console.WriteLine("Tails");
                tailCount++;
            }
        }
        Console.WriteLine("Heads came up {0} times.", headCount);
        Console.WriteLine("Tails came up {0} times.", tailCount);
        if (headCount > tailCount)
        {
            Console.WriteLine("Heads wins.");
        }
        else
        {
            Console.WriteLine("Tails wins."); …
Run Code Online (Sandbox Code Playgroud)

c# random for-loop

1
推荐指数
1
解决办法
6277
查看次数

标签 统计

c# ×1

for-loop ×1

random ×1