C#randomguess | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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

26th Jan 2022, 5:36 AM
Lenoname
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.
26th Jan 2022, 10:34 PM
Godfred Opintan
Godfred Opintan - avatar
+ 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.
26th Jan 2022, 5:56 AM
Ipang
+ 1
Ipang thats the only issue? Or something wrong with the code?
26th Jan 2022, 6:20 AM
Lenoname
26th Jan 2022, 7:59 AM
Lenoname
+ 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
26th Jan 2022, 9:27 AM
Ipang
+ 1
And don't forget your semi colons. If you add them to your code, you will get a syntax error
26th Jan 2022, 7:38 PM
Godfred Opintan
Godfred Opintan - avatar
26th Jan 2022, 8:51 PM
Lenoname
+ 1
Yes, just one thing though. The random number range, it still says from 0 ~ 100 : ) Good job! 👍
27th Jan 2022, 12:27 AM
Ipang
0
Can't check the code at its current state buddy, it is not even running ...
26th Jan 2022, 7:40 AM
Ipang