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) Perform if / else

Li Yu
Li Yu
2,067 Points

Problem of if/else

I thought when typing string entry = Console.ReadLine(); in the first line already means that whatever words are entered, the words will show up as the string language. What is the problem in this program? And how could I print out the words that are entered? Thanks for the reply!

CodeChallenge.cs
string language = Console.ReadLine();

if(language == "C#")
{
  System.Console.WriteLine("C# Rocks!");
}
else
{
  System.Console.WriteLine(language + "is not C#");
}

1 Answer

andren
andren
28,558 Points

Your code is fine, language is equal to what was entered into to the program. But one thing to take notice of is that when you combine strings together they are merged without adding any spaces between them. Therefore in order to produce a sentence with proper spacing you have to add one manually. Either by adding a string that just contains a space, like this:

language + " " + "is not C#"

Or by adding a space at the start of the next string like this:

language + " is not C#"

Either way is fine, but you do have to do something to produce that space. The challenge checker for these challenges is pretty strict about text output. If it asks for a sentence with certain spacing it will mark your code as wrong it doesn't get exactly the sentence it wanted.

Li Yu
Li Yu
2,067 Points

andren, thank you so much! It is so exciting to pass this code challenge, and thank you very much for the detailed explanation.