随机数发生器C#

时间:2015-11-06 21:58:29

标签: c#

我正在尝试让我的程序以20个生命启动用户,而当Lives大于0时,翻转硬币(随机数生成器0(Tails)或1(Heads))。每次用户获得Heads时,它会向Heads计数器添加1,向Flips计数器添加1。每次用户获得Tails时,它从Lives中获取1并将1添加到Flips计数器。我想我唯一的问题是我的随机数发生器!?请善待,总计n00b:)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Coin_Flip_2015
{
    public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }
   private Random r = new Random(1);
    private void cmdFlipCoin_Click(object sender, EventArgs e)
    {

        int rand = r.Next(1);
        int Heads;
        int counterFlips;
        int Lives;

        Lives = 20;
        Heads = 0;
        counterFlips = 0;

        while (Lives > 0)
            if (rand == 0)
            {
                Lives = Lives -= 1;
                counterFlips++;
            }

            else if (rand == 1)
            {
                Heads = Heads += 1;
                counterFlips++;
            }

        lblFlips.Text = "Flips = " + counterFlips.ToString();
        lblHeads.Text = "Heads = " + Heads.ToString();
        lblLivesLeft.Text = "Lives Left = " + Lives.ToString();
        MessageBox.Show("sorry you are out of lives m8");
        }
    }
}

Form.png

3 个答案:

答案 0 :(得分:1)

Random.Next(int)方法

  

返回小于指定最大值的非负随机整数。

因此r.Next(1)始终为0.使用r.Next(2)获取0或1。

您还应该在rand = r.Next(1);循环内移动while。现在它只在方法的开头分配一次。

答案 1 :(得分:1)

我会解决所有问题:

public partial class Form1 : Form
{
    int Heads, counterFlips, Lives;

    public Form1()
    {
        InitializeComponent();
        Lives = 20;
        Heads = 0;
        counterFlips = 0;
    }

    private Random r = new Random();

    private void cmdFlipCoin_Click(object sender, EventArgs e)
    {
        int rand = r.Next(2);

        if (Lives > 0)
        {
            if (rand == 0)
            {
                Lives -= 1;
                counterFlips++;
            }
            else
            {
                Heads += 1;
                counterFlips++;
            }
        }
        else
            MessageBox.Show("sorry you are out of lives m8");

        lblFlips.Text = "Flips = " + counterFlips.ToString();
        lblHeads.Text = "Heads = " + Heads.ToString();
        lblLivesLeft.Text = "Lives Left = " + Lives.ToString();
    }
}

的问题:

  • HeadscounterFlipsLives变量存储在本地,需要全局
  • 代码已更改,因此每次单击按钮时都会翻转硬币(将while更改为if)。不应在UI线程中使用具有永不退出功能的while循环。
  • private Random r = new Random()通知删除了1,使用默认构造函数正确播种。
  • 添加了" else"显示生命的使用时间,它会显示您的信息
  • Lives = Lives -= 1(与Heads相同)更改为正确的代码
  • 使用r.Next(2)获取0或1,因为上限是独占的。

除此之外,我不确定您使用LivesHeads号码做了什么,所以我会留给您修复。

答案 2 :(得分:1)

这里有一点不同.. snap 几秒钟就打败了我!

public partial class Form1 : Form
{
    // Leave the seed alone on this line
    private readonly Random _r = new Random();

    // these need to be outside of your Click event
    private int _counterFlips;
    private int _heads;
    private int _lives = 20;

    public Form1()
    {
        InitializeComponent();
    }

    private void cmdFlipCoin_Click(object sender, EventArgs e)
    {
        // x = starting number, y = ending number + 1
        // This should be either a zero or one:
        int rand = _r.Next(0, 2);

        switch (rand)
        {
            default:
                _lives --;
                _counterFlips++;
                break;
            case 1:
                _heads ++;
                _counterFlips++;
                break;
        }

        if (_lives < 1)
        {
            MessageBox.Show(@"sorry you are out of lives m8");
            button1.Enabled = false;
            return;
        }


        lblFlips.Text = @"Flips = " + _counterFlips;
        lblHeads.Text = @"Heads = " + _heads;
        lblLivesLeft.Text = @"Lives Left = " + _lives;

    }
}