0

好的,所以我正在编写一个程序,它应该只允许用户单击某个按钮随机次数(1-25 次),这是每次用户启动程序时生成的。我的代码对此不起作用,该程序当前在 ListBox 中保留一个总计值,当用户点击他们可以点击按钮的“限制”数量时,它假设将该总计值写入我已经保存的文件. 另外,当激活此“随机次数”时,您将如何合并消息显示?这是我的代码,谢谢:

     public Form1()
       {
        InitializeComponent();
        }
        private int lose()
        {

        // int High_Score;

        Random rand = new Random();
        lost = rand.Next(23) + 2;
        return lost;
        MessageBox.Show(" you lose");


       }

       private void begingameButton_Click(object sender, EventArgs e)
        {   
        lose();
        //Begin game button disable
        begingameButton.Enabled = false;
        //The roll button is enabled
        rolldieButton.Enabled = true;
        //Makes the beginning instructions visible
        beginLabel.Visible = false;
        //Makes the other instructions visible
        diceBox1.Visible = true;
        //Enables the start over button
        startoverButton.Enabled = true;
        //Displays a total of 0 dollars at the beginning of game
        int beginTotal = 0;
        totalmoneyLabel.Text = beginTotal.ToString("c");

         }
4

1 回答 1

0

您只需要初始化Random一次类,否则每次调用都会得到相同的数字.Next()

假设滚动按钮是需要点击次数有限的按钮......

private Random Rand = new Random();

private int RollCount;
private int RollLimit;

private void begingameButton_Click(object sender, EventArgs e)
{
    RollCount = 0;
    RollLimit = Rand.Next(1, 25);

    //...other prep work
}

private void rolldieButton_Click(object sender, EventArgs e)
{
    RollCount++;

    if (RollCount == RollLimit)
    {
        // limit reached
    }
}
于 2012-11-01T04:09:59.807 回答