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 trialThomas Matlock
2,900 PointsThink about how to properly declare a variable and variable scope, then fix the following code so that it compiles.
I am trying figure out what needs to be moved around in order to make this code compile. This section is about variable scope and variables and I am not understanding variable scope
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
input = Console.ReadLine();
if (input == "quit")
{
string output = "Goodbye.";
}
else
{
string output = "You entered " + input + ".";
}
Console.WriteLine(output);
}
}
}
4 Answers
Steven Parker
231,269 PointsHere's a few hints:
- a variable should be declared before (or when) it is assigned
- a variable should only be declared one time, but can be assigned any number of times
- a variable should be declared in the outermost scope in which it is used
Matthew Musni
610 Pointsit's better to click on the preview and see what the problem is... it seems that it has a problem in the input variable
as a result it must be like this
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
string input = Console.ReadLine();
if (input == "quit")
{
Console.WriteLine("Goodbye.");
}
else
{
Console.WriteLine("You entered " + input + ".");
}
}
}
}
i even deleted the last method at the bottom and instead put the method Console.WriteLine under if and else.
Steven Parker
231,269 PointsThe challenge instructions say "Be sure to not change the intent or intended behavior of the code.". While this modification does exhibit the same behavior, it alters the intent of providing an opportunity to properly scope the output variable.
Matthew Musni
610 Pointsohh can I see what the code should like. I got no ideas. thanks!
Vasy Florea
Courses Plus Student 6,367 Pointsusing System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
string input = Console.ReadLine();
string output = input == "quit" ? "Goodbye":"You entered " + input + ".";
Console.WriteLine(output);
}
}
}