hi im new to programming and i was told to create a dice game where a player rolls and the dice rolled will added to the total score until the score is reached its maximum limit but i cant get it to work.. my problem is i cant keep hold of the score. any suggestions ?

``````Random randomnumber = new Random();
int dice1 = randomnumber.Next(1, 7);
lblDice.Text = dice1.ToString();
int score = Convert.ToInt32(dice1.ToString());
Console.WriteLine(dice1);

if (dice1 >= 1)
score = + dice1;
lblplayer1score.Text = score.ToString();``````

## All 2 Replies

First a couple of observations...
-By calling `randomnumber.Next(1, 7)` you will receive a "random" number whose minimum value is 1, and whose maximum value is 7. Thus, your conditional statement in line 7 from your post will always evaluate true.
-In line 4, think about how the computer is going to execute this statement: `int score = Convert.ToInt32(dice1.ToString());` . The first thing that will happen is that the value of `dice1` (which is an integer) will be converted to a string. Then, you're converting that string back into an integer. `score` and `dice1` are already of the same type. You don't need to do any conversions.

Now for the problem you're having keeping the score, it has to do with the scope of your `score` variable. I'm assuming the code you posted is either in a loop, or a method, or something similar. Try setting a breakpoint in your code where you first set the value of `score` , then when debugging, watch `score` As you go through different iterations of the loop/method. You should notice that for every iteration, a new integer variable called "score" is being created. This isn't what you want, since you want `score` to exist between rolls of the die. So, the scope of `score` is too narrow: it needs to be declared somewhere higher than this loop/method.

If you have this code in a loop, then declaring `score` somewhere above the loop will keep it persistent during different iterations of the loop. If it's a method, then simply pass the method the score object (as a pointer), have the method return the new score after each call, etc. There's a number of ways to handle this.

First a couple of observations...
-By calling `randomnumber.Next(1, 7)` you will receive a "random" number whose minimum value is 1, and whose maximum value is 7.

Actually, the upper bound is exclusive, so the number is from 1 to 6 (Random.Next(Int32, Int32)).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.