for loop dice roll and textbox.Text updating troubles c#

大憨熊 提交于 2019-12-25 14:33:26

问题


I'm trying to make a program where there is at first a random dice roll that is "rolls" to decide the number of times that dicethrow should throw the dice (between 1 and 9 times), anything greater than rolls should be the end of the game. I also need the score to update after every roll, which is what I was trying to do in the comments, but I'm not sure if I would need to TryParse the wagerTextBox.Text to get a value and prevent format exceptions, or if it would be fine without it (or where I would put the TryParse).

#region private method randomdiceroll
private void rollButton_Click(object sender, EventArgs e)
{
    rollDice();
    wagerTextBox.Enabled = false;
}
private int RollsNumber()
{
    Random rolls = new Random();
    return rolls.Next(1, 10);
}
private int diceThrow()
{
    Random dice = new Random();
    return dice.Next(1,7);
}
private void rollDice()
{
   int i = RollsNumber();
   for (i = 0; i <= 10; i++)
   {
       diceThrow();
       int wager = Convert.ToInt32(wagerTextBox.Text);
       int score = wager * 100;
       scoreTextBox.Text = Convert.ToString(score);
       {
          // wagerTextBox.Text = null;
         //  wagerTextBox.Text = scoreTextBox.Text;
       }
   }
}
#endregion

回答1:


To solve the issue of rolling a random number of times switch this code:

int i = RollsNumber();
for (i = 0; i <= 10; i++)
{

to:

int i;
int maxRolls = RollsNumber();
for (i = 0; i < maxRolls; i++)
{

for your second part of the question, I have no idea what exactly you are trying to accomplish, sorry.



来源:https://stackoverflow.com/questions/13168977/for-loop-dice-roll-and-textbox-text-updating-troubles-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!