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

Manish Barik
PLUS
Manish Barik
Courses Plus Student 2,071 Points

I can't understand what's wrong with this code

I am stuck in this if/else temperature quiz. Please help. I am unable to figure out what's wrong with my code.

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

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Actually, there's nothing weird about what the challenge asks for, there're different ways to write up conditional clauses to solve this problem, you were on the right track for sure, but unfortunately, all 3 of your conditionals were incorrect. On top of that, your output String needs proper spacing & capitalization to match the test cases used by the grader.

If you slightly modify the posted code, it'd pass the challenge without problem.

string input = Console.ReadLine();
int temperature = int.Parse(input);
if (temperature >= 21 && temperature <= 22)  // temperature between 21 ~ 22
{    Console.WriteLine( "Just right." ) ;   // fixed spacing in output string
}
else if (temperature < 21)                   // temperature less than 21
{
Console.WriteLine( "Too cold!" );          // fixed spacing & capitalization
}
else if (temperature > 22)                     // temperature greater than 22
{
Console.WriteLine( "Too Hot!" );          // fixed spacing
}

Hope it helps.

Manish Barik
Manish Barik
Courses Plus Student 2,071 Points

I tried that code first. But it didn't work. Then I tried using >= which is definitely incorrect. However, as it was showing incorrect despite entering the correct coding like yours, I thought to better post this on community. Thanks, for the reply

I like your answer and originally tried it. Last night my challenges were bugging out (resizing/changing shapes) and many answers were not working, I am glad it is working now.

William Li
William Li
Courses Plus Student 26,868 Points

hi, Philip Gales

Team Treehouse developers are ware of the code challenge issue you mentioned for quite some time, I guess the reason why it hasn't been fixed by now probably because it's a difficult problem to pinpoint, it's only experienced by a small number of students on some OS using certain web browsers.

Thanks for helping out in the forum.

They want you to do this a weird way, not sure why.

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

if (temperature < 21)
{
  Console.WriteLine("Too cold!");
}
else if (temperature > 22)
{
  Console.WriteLine("Too hot!");
}
else 
{
  Console.WriteLine("Just right.");
}
Manish Barik
Manish Barik
Courses Plus Student 2,071 Points

This is definitely a 'weird' way. But, simple. Thanks, Philip. Glad, you are awake ;)