+ 1

C#guess

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.My attempt at this: https://code.sololearn.com/cFv5aNA8bYjY/?ref=app

24th Jan 2022, 6:27 PM
Lenoname
5 Answers
+ 1
using System; namespace Sololearn { class Program { static void Main(string[] args) { //wrap all your code into main method lije this Console.WriteLine("guess a number between 1 and 100"); int x = Convert.ToInt32(Console.ReadLine()); //missing () if (x==60){ Console.WriteLine("your guess is right"); } //if the user chooses numbers between 55-59,not even sure i should be writing it like this else if (55 < x && x <59){ // use logical and , your statement 55<x<59 is not valid in c# Console.WriteLine("wrong but close, guess a higher number!"); } else if (61 < x && x < 65){ Console.WriteLine("wrong but close, guess a lower number!"); } //other numbers higher than 100 or lower than 0 else if (x <= 0 || x >= 100){ // Console.WriteLine("you can only guess a number between 1 and 100"); } else{ Console.WriteLine("your guess is wrong"); } Console.WriteLine("click enter to close"); Console.ReadLine(); } } } /* edit: yes. else part is ok. sry I may read it as x<=100 Lenoname */
24th Jan 2022, 6:40 PM
Jayakrishna šŸ‡®šŸ‡³
+ 4
Yes, it's working fine as you expected. https://code.sololearn.com/cG0jXJMNZ1LJ/?ref=app
25th Jan 2022, 7:39 AM
Simba
Simba - avatar
+ 3
You can combine last two else if statements as single else if using logical OR(||) operator else if (x <= 0 || x >= 100){ Console.WriteLine("you can only guess a number between 1 and 100"); } else....
25th Jan 2022, 2:47 AM
Simba
Simba - avatar
+ 1
Why should i remove the else {Console.WriteLine(ā€your guess i wrongā€);}? Lets say the user chooses number 30 which is not close to 60, how should i tell the user he/she chose the wrong answer?
24th Jan 2022, 6:57 PM
Lenoname
+ 1
Else part is ok?? I mean the last else statement.
25th Jan 2022, 6:57 AM
Lenoname