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# Objects Loops and Final Touches Constants

olu adesina
olu adesina
23,007 Points

i thought i had answered question, but it saying "Did you change the structure of the 'if / else if / else' statement

Did you change the structure of the 'if / else if / else' statement?

why do i need to change the structure?

CodeChallenge.cs
const int revenue = 125000;
string status = null;
const int oneHundred_K = 100000;
const int one50_k =  150000;
const string red = "red"
const string yellow = "yellow"
const string green = "green"    

if (revenue < oneHundred_K )
{
    status = red;
}
else if (revenue <one50_k)
{
    status = yellow;
}
else
{
    status = green;
}

WE just need to replace the magic numbers and strings with constants (const) and declare them before the if/else statements and those magic numbers and strings with these constants. we can declare them like this:

const int a = 100000; const int b = 150000; const string c = "red"; const string d = "yellow"; const string e = "green";

Then just replace 100000, 150000, "red", "yellow', and "green" with a, b, c, d, and e.

3 Answers

const int revenue = 125000; string status = null; const int a = 100000; const int b = 150000; const string c ="red"; const string d = "yellow"; const string e = "green";

if (revenue < a) { status = c; } else if (revenue < b) { status = d; } else { status = e; }

We deal the constants (const) to be easy to change them once without going through our programs and change the numbers and strings, so it will be easy and time saving when we use constants.

Rajmohan Ganapathi
Rajmohan Ganapathi
1,405 Points

Just remove the space in the if statement "if (revenue < oneHundred_K )" then you will not get the error.