C#randomguess | Sololearn: Learn to code for FREE!



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


1/26/2022 5:36:49 AM


9 Answers

New Answer


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.


Your code is invalid, I suggest you create a new C# code and place this code inside the Main method of the new code.


Ipang thats the only issue? Or something wrong with the code?


Ipang now? https://code.sololearn.com/cOrqW8gUPWoY/?ref=app


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


And don't forget your semi colons. If you add them to your code, you will get a syntax error


Ipang like this: https://code.sololearn.com/cOrqW8gUPWoY/?ref=app


Yes, just one thing though. The random number range, it still says from 0 ~ 100 : ) Good job! 👍


Can't check the code at its current state buddy, it is not even running ...