使用随机值从另一个方法访问另一个变量

时间:2015-03-23 23:43:05

标签: c#

我创建了一个整数列表。我创建了一种从列表中选择一个随机数并使用文本框输出的方法。一旦在列表中挑选它,它将被删除,以便不会重复。这是我的代码:

private void clickHandler(object sender, EventArgs e)
{
    if (btnCase.Text == "")
    {
        lblSelectedCase.Text = "Your Briefcase:";
        lblCasesToOpen.Visible = true;
        lblNumber.Visible = true;
        lblNumber.Text = myGame.counter.ToString();
        btnCase.Visible = true;
        btnCase.Text = ((Button)sender).Text;
        ((Button)sender).Visible = false;
    }
    else
    {
        lblNumber.Text = myGame.decreaseCounter().ToString();
        num = myGame.showAmount();

        var selected = MessageBox.Show("Briefcase amount: $" + num.ToString(), "", MessageBoxButtons.OK);

        if (selected == DialogResult.OK)
        {
            _label[num].Visible = false;
            myGame.removeAmount();
            ((Button)sender).Visible = false;
        }

        if(myGame.counter == 0)
        {
            foreach (Button button in this.Controls.OfType<Button>())
            {
                button.Enabled = false;
            }

            myGame.computeAverage();
            lblBankerOffer.Visible = true;
            lblOffer.Visible = true;
            lblOffer.Text = "$" + myGame.bankerOffer().ToString();
            btnDeal.Visible = true;
            btnNoDeal.Visible = true;
            lblCasesToOpen.Visible = false;
            lblNumber.Visible = false;
        }
    }
}

private void btnNoDeal_Click(object sender, EventArgs e)
{
    if (myGame.turn == 9)
    {
        int n = num;
        var winnings = MessageBox.Show("You won: $" + n.ToString(), "Congratulations", MessageBoxButtons.OK);

        if (winnings == DialogResult.OK)
        {
            if (n > myData.Score)
            {
                myData.Score = myGame.showAmount();
                myData.updateScore();
            }

            this.Hide();

            frmHome _frmHome = new frmHome(myData.User.ToString(), myData.FName.ToString(), myData.LName.ToString(), myData.Score);

            _frmHome.Closed += (s, args) => this.Close();
            _frmHome.Show();
        }
    }
    else
    {
        foreach (Button button in this.Controls.OfType<Button>())
        {
            button.Enabled = true;
        }

        lblNumber.Text = myGame.showCounter().ToString();
        lblBankerOffer.Visible = false;
        lblOffer.Visible = false;
        btnDeal.Visible = false;
        btnNoDeal.Visible = false;
        lblCasesToOpen.Visible = true;
        lblNumber.Visible = true;
    }
}

我想访问clickHandler方法num = myGame.showAmount();的部分到我的btnNoDeal_Click,因为我在转弯== 9时遇到问题,会出现一个MessageBox,给我一个已经使用过的值

编辑:显示金额方法是列表中随机生成的数字:

public int showAmount()
{
    index = _random.Next(0, amounts.Count);
    return amounts[index];
}

0 个答案:

没有答案