- 2
C#randomguess
How do i generate a random number between 1-100 then ask the user to guess the number? If the users guess is close ,the programme should tell the user that she is close and she should either raise or lower it, otherwise tell her that her answer is wrong, the user shouldnt guess above 100 or below 0 and would be alerted if she did. This is my attempt, whats lacking?: https://code.sololearn.com/cQ40m77xAO0y/?ref=app
9 Answers
+ 3
I reviewed your code and fixed some errors
https://code.sololearn.com/c28U0W0A89mV/?ref=app
Unlike before, it's works. Haven't made any changes to your code.
+ 1
Your code is invalid, I suggest you create a new C# code and place this code inside the Main method of the new code.
+ 1
Ipang thats the only issue? Or something wrong with the code?
+ 1
Please use meaningful variable names. Instead of <x> and <y>, you can name them <randomNum> and <userGuess>.
Line 14:
I thought you wanted to generate a random value between 1 ~ 100. But there I see rand.Next(0,101)
To simplify the conditionals, you can calculate the distance between <randomNum> and <userGuess>. Use Math.Abs() to get the distance
// distance between <randomNum> and <userGuess>
int distance = Math.Abs( randomNum - userGuess );
// is the <userGuess> near the <randomNum>
bool near = ( distance <= 5 );
And then the conditionals (in pseudo)
if userGuess > randomNum
if near
// wrong but close, guess lower
else
// wrong, guess lower
else if userGuess < randomNum
if near
// wrong but close, guess higher
else
// wrong, guess higher
end if
+ 1
And don't forget your semi colons. If you add them to your code, you will get a syntax error
+ 1
+ 1
Yes, just one thing though. The random number range, it still says from 0 ~ 100 : )
Good job! 👍
0
Can't check the code at its current state buddy, it is not even running ...