简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM