C# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C#

The user has to guess a number between 1-100, if he/she is close to the answer but not correct the programme will tell him that the answer is wrong but he is close. All the other answers will result in the wrong answer, for example numbers over 100 or under 0 or numbers not close to the answer. The random number in this case is 60 which the user should guess. https://code.sololearn.com/cFv5aNA8bYjY/?ref=app

22nd Jan 2022, 1:41 AM
Lenoname
5 Answers
+ 2
if (100 < x <0) tests if a number is lower than zero and, at the same time, greater than 100, so will always be false. This needs rethinking. Two hints to make it all easier: 1. Use comparisons consistently - don't jump from greater to lower and back. If you stick to greater or lower in most comparisons, the code gets more readable. 2. Make the comparisons from the lowest to the highest, or the opposite. You can make simpler comparisons this way.
22nd Jan 2022, 3:22 AM
Emerson Prado
Emerson Prado - avatar
+ 2
A bonus fun: replace 60 for an integer variable. This way, you can test other numbers, or even randomize, without much effort.
22nd Jan 2022, 3:24 AM
Emerson Prado
Emerson Prado - avatar
0
So just 0 < x then 100 < 0 in another else if statement?
23rd Jan 2022, 10:09 PM
Lenoname
0
Could you pls explain? I can't understand the last message.
23rd Jan 2022, 11:28 PM
Emerson Prado
Emerson Prado - avatar
0
U said 100 < x < 0 would turn false every time , so if i instead write: Else if (x<0){some commands;} Else if (x>100){some command;} That would be alright, right?
23rd Jan 2022, 11:43 PM
Lenoname