0
Guess
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. Integer variable is something like int x ; but then what do i replace the user input in an if statement? And how? For example if (input is close to x){do this;} What shld be instd
6 Respostas
+ 5
How far off the guess is from the random number can be obtained as the difference between the guess and the actual number. Consider:
random number: 39
user guess: 50
39-50 is -11
In this context, a negative number indicates that the guess is greater than the target, while a positive number indicates that the guess is smaller than the target.
You can then decide how close is 'close', by setting a threshold. If the absolute difference is larger than 30, for example, then you may not consider it a close guess. Otherwise, you can hint towards whether the guess should be greater or lesser than the previous input.
+ 3
Lenoname There are pretty good examples on how to generate random numbers in c#. You can search the Q&A for them. :>
https://www.sololearn.com/Discuss/127197/?ref=app
As for calculating difference, either target-guess or guess-target works. What differs between the two options is how your evaluate the resulting value. For target-guess, a negative number indicates guess being larger than target. The opposite is true if you do guess-target.
+ 1
... Just set and use it like you would any other variable?
int threshold = 30;
// Take input, generate random target
// Calculate difference
// check if guess is correct
// otherwise...
else if (difference <= threshold)
// Output "You are close!"
if (difference < 0)
// Output "Target is greater than guess"
else
// Output "Target is lesser than guess"
0
How do i put that threshold??
0
Hatsy Rei I understood your if, else if and else statements, and i know how to take input with Console.ReadLine(); but how do i generate a random number?? And calculate the dif how? Target-guess?Guess-target(if the guess is greater than the actual number)
0
Hatsy Rei what do u think about this?
https://code.sololearn.com/cQ40m77xAO0y/?ref=app