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

Michael Bragg
Michael Bragg
811 Points

Non-Compilation

My code compiles in Visual Studio, but I get bummer here

using System;

namespace Temerature { class Program { static void Main() { string input = Console.ReadLine(); int temperature = int.Parse(input);

        if (temperature < 21)
        {
            System.Console.WriteLine("Too cold!");
        }
        else if (temperature >= 21 && temperature < 22)
        {
            System.Console.WriteLine("Just right.");
        }
        else
        {
            System.Console.WriteLine("Too hot!");
        }
    }
}

}

CodeChallenge.cs
using System;

namespace Temerature
{
    class Program
    {
        static void Main()
        {
            string input = Console.ReadLine();
            int temperature = int.Parse(input);


            if (temperature < 21)
            {
                System.Console.WriteLine("Too cold!");
            }
            else if (temperature >= 21 && temperature < 22)
            {
                System.Console.WriteLine("Just right.");
            }
            else
            {
                System.Console.WriteLine("Too hot!");
            }
        }
    }
}

14 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Michael Bragg! First, and foremost, these challenges generally require that you not do anything they haven't asked for. In your code, you've included all the code that was necessary to compile your program. This includes the directive to use System, a namespace, and even a main function. That is already set up for you in the challenge. All it wants you to do is enter the small code snippet it's asking for. The code it's looking for is between the line with string input and the end of the else statement.

Once you put that code into the challenge, you will still get a Bummer! message. There's a small logic flaw in else if statement. It should print out "Just right." if the temperature is 22.

Hope this helps! :sparkles:

Michael Bragg
Michael Bragg
811 Points
string input = Console.ReadLine();
int temperature = int.Parse(input);
if("temperature < 21)
   {
    System.Console.WriteLine("Too cold!");
    }
   else if("temperature == 22);
    {
    System.Console.WriteLine("Just right!");
    }
    else
           {
            System.Consolte.WriteLine("Too hot!");
            }

Still get the bummer error

Moderator edited: Markdown added so that code renders properly in the forums.

Michael Bragg
Michael Bragg
811 Points
string input = Console.ReadLine();
int temperature = int.Parse(input);

if("temperature < 21")
   {
    System.Console.WriteLine("Too cold!");
    }
   else if("temperature == 22);
    {
    System.Console.WriteLine("Just right!");
    }
    else
    {
    System.Console.WriteLine("Too hot!");
    }

Moderator edited: Markdown added so that code renders properly in the forums.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi again! Now you have some syntax errors. Because you've included your temperature < 21 in quotation marks, it is now a string instead of an expression. You will need to remove the quotation marks there. You also have a stray quotation mark in your else if statement. The code should print out "Just right" if the temperature is 21 or 22.

Michael Bragg
Michael Bragg
811 Points
string input = Console.ReadLine();
int temperature = int.Parse(input);

if("temperature < 21")
   {
    System.Console.WriteLine("Too cold!");
    }
   else if("temperature == 22");
    {
    System.Console.WriteLine("Just right!");
    }
    else
    {
    System.Console.WriteLine("Too hot!");
    }

Moderator edited: Markdown added so that code renders properly in the forums.

Michael Bragg
Michael Bragg
811 Points
string input = Console.ReadLine();
int temperature = int.Parse(input);

if(temperature < 21)
   {
    System.Console.WriteLine("Too cold!");
    }
   else if(temperature == 22);
    {
    System.Console.WriteLine("Just right!");
    }
    else
    {
    System.Console.WriteLine("Too hot!");
    }
       Not a string any longer and still bummer

Moderator edited: Markdown added so that code renders properly in the forums.

Michael Bragg
Michael Bragg
811 Points
string input = Console.ReadLine();
int temperature = int.Parse(input);

if(temperature < 21)
   {
    System.Console.WriteLine("Too cold!");
    }
   else if(temperature == 22);
    {
    System.Console.WriteLine("Just right!");
    }
    else
    {
    System.Console.WriteLine("Too hot!");
    }

Moderator edited: Markdown added so that code renders properly in the forums.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Spamming this thread will not make anyone more inclined to give you an explicit answer. Again, the code should print out "Just right" if the temperature is 21. It should also print out "Just right" if the temperature is 22. Either one of those temperatures fall in the "Just right" range.

Michael Bragg
Michael Bragg
811 Points
string input = Console.ReadLine();
int temperature = int.Parse(input);

if(temperature < 21)
   {
    System.Console.WriteLine("Too cold!");
    }
   else if(temperature == 22);
    {
    System.Console.WriteLine("Just right.");
    }
    else
    {
    System.Console.WriteLine("Too hot!");
    }

with correct punctuation even Bummer

Moderator edited: Markdown added so that code renders properly in the forums.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Please read what I have written repeatedly. Your code currently prints out "Just right." when the temperature is 22. It should also print out "Just right." when the temperature is 21. What does your code currently print out when the temperature is 21? It prints out "Too hot!".

Michael Bragg
Michael Bragg
811 Points

How am I spamming posting my code Jennifer?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Personally, I feel like posting within a few seconds of each other while completely disregarding what was written back to you several posts up could be considered spamming.

Michael Bragg
Michael Bragg
811 Points

Maybe I am behind in the posts, but my code now reads as you suggested

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

The last piece of code that you posted contains this: else if(temperature == 22);.

But again, the code should print out "Just right." if the temperature is 22 or 21. Either one. But currently, your code only prints out "Just right." when it's 22.

Michael Bragg
Michael Bragg
811 Points

Jennifer, I am making changes to my code and posting updates.

Michael Bragg
Michael Bragg
811 Points

I have corrected what you suggested:

string input = 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!");
    }

Moderator edited: Markdown added so that code renders properly in the forums.

Michael Bragg
Michael Bragg
811 Points

...html

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

if(temperature < 21)
   {
    System.Console.WriteLine("Too cold!");
    }
   else if(temperature >= 21 && temperature <= 22);
    {
    System.Console.WriteLine("Just right.");
    }
    else
    {
    System.Console.WriteLine("Too hot!");
    }

Moderator edited: Markdown added so that code renders properly in the forums. ...

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Ok now it's correct except for one semicolon. You've placed an extra semicolon at the end of the expression in the else if. Removing that will cause it to pass.

Michael Bragg
Michael Bragg
811 Points

...html <p> string input = Console.ReadLine(); int temperature = int.Parse(input);

if(temperature < 21) { System.Console.WriteLine("Too cold!"); } else if(temperature >= 21 && temperature <= 22); { System.Console.WriteLine("Just right."); } else { System.Console.WriteLine("Too hot!"); } </p> ...

This is my current code and still Bummer

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Again, you have an extra semicolon at the end of the expression in the else if that is causing a syntax error. Removing that will cause the challenge to pass.

Michael Bragg
Michael Bragg
811 Points

Thank you, Thank you, Thank you.