Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Sean Brannon
Sean Brannon
596 Points

Cant find the error in my code for this challenge.

completely stumped on this one, i cant find the error and i am not even sure if i am the challenge right in the first place.

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

1 Answer

I see a couple of things here: this line else if (temperature == 21,22) has 2 values to compare, the syntax is incorrect if you wish to compare more values use the "and" "or" && || keywords. The last else if statement looks for a number equal or greater than 22, but the previous else statement is also looking for 22, so you need to rethink where the values should fall. I hope this helps, cheers.

Sean Brannon
Sean Brannon
596 Points

Thank you so much, i kept focusing on the wrong things, it took me a while (and a little google) to figure out what i was doing wrong.

didn't realize i was supposed to put temperature in twice.