Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perfect if / else if / else

Ankit Biswas
Ankit Biswas
280 Points

else if problem.

The following table describes my room temperature preferences. Print the message from the table when a user enters a number in the corresponding range. For example, if temperature is 21 the code should print "Just right." to the screen.

Temperature (°C) Message Less than 21° Too cold! 21° to 22° Just right. Greater than 22° Too hot! Please tell me the write code for it.

CodeChallenge.cs
string input = System.Console.ReadLine();
int temperature = int.Parse(input);
if(temperature<21)
{
    System.Console.WriteLine("Too Cold!");
}
else if(temperature==21,22)
{
    System.Console.WriteLine("Just right.");
}
else
{
    System.Console.WriteLine("Too hot!");
}

1 Answer

Patricia Hector
Patricia Hector
42,901 Points

I would check if the variable temperature is less than 21, then if it is greater than 22; and if none of these two conditions are met, that means that the value temperature must be 21 or 22.

string input = Console.ReadLine();
int temperature = int.Parse(input);

if(temperature<21){
    System.Console.WriteLine("Too cold!");
} else if(temperature>22){
    System.Console.WriteLine("Too hot!");
} else{
    System.Console.WriteLine("Just right.");
}
Patricia Hector
Patricia Hector
42,901 Points

The problem with your code is that you are comparing the variable temperature with the decimal value 21,22. You can use your code if you change that line for this one;

else if(temperature==21 || temperature==22){
       System.Console.WriteLine("Just right.");
}

Here this condition will be true just when the variable temperature is either equal to 21 or 22.