Im new to csharp and came across a problem while writing a Guessing Game where you have 10 attempts. Could anyone tell me what im doing wrong? It would be much appreciated.
numberGen is Random()
.
numberGen.Next(1, 11) is Int
.
guess is Int
.
I think its pretty clear.
Ohh, Thank you! My bad, I didnt see it haha
NumberGen.nextInt or next() gives you a random number, otherwise it’s a random generator
Side note: the for-loop doesn’t do what you think.
The error is saying you have some !=
where one side is an int
and the other side is a Random
object. To debug, you look for cases where you have a int != Random
situation. Granted, you may not know that numberGen is a Random
but you should have a good guess of where the problem could be due to while (guess != numberGen)
being the only !=
you have. Then you could search google for “C# Random use” for tips on how to use it.
What you need to do, as pointed out, is to use numberGen to generate a number. Random
is a class for generating random numbers, not a random number of its own. You need only make one of them and then each time you call the method numberGen.Next(1, 11)
you will get a new random number equal to or greater than the first number (1) and strictly less than the second number (11). Since it is an integer, that means no less than 1 and no greater than 10.
Your for loop isn’t going to work. Well, it will loop, but it won’t do anything. For loops execute the code contained in a following {...}
bracket set. You don’t have the brackets, you just have a semicolon. That is going to tell the system to run your loop, sure, but it won’t have anything to execute.
A tip is to move the Random numberGen = new Random();
line up one step, fix the !=
error, remove the semicolon after the for loop line, and then wrap everything under it with curly brackets. You will notice a problem when you run it but should be able to fiddle with what is and isn’t inside the loop to get it how you’d like.
Good luck!
C# devs
null reference exceptions