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 trialManish Barik
Courses Plus Student 2,071 PointsI 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.
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
Courses Plus Student 26,868 PointsActually, 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.
Philip Gales
15,193 PointsThey 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
Courses Plus Student 2,071 PointsThis is definitely a 'weird' way. But, simple. Thanks, Philip. Glad, you are awake ;)
Manish Barik
Courses Plus Student 2,071 PointsManish Barik
Courses Plus Student 2,071 PointsI 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
Philip Gales
15,193 PointsPhilip Gales
15,193 PointsI 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
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 Pointshi, 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.