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 trialbrevans26
15,198 PointsDifferent Solution
I used a different approach. Actually, the only thing I did differently was to move the "Goodbye"
line inside of the if else statement. In my head I didn't see a purpose of evaluating keepGoing
to false
. Is there any problem with that?
Looking now at my code, I see I could have tried to invert the if...else
statements in a way that Goodbye!
would show at the end of the code.
Second, I put my curly braces in the same line where I declare the state, class and etc. Any problem with that?
Here is my code:
using System;
namespace Treehouse.PracticeSession {
class Program {
public static void Main() {
bool keepGoing = true;
while (keepGoing == true) {
System.Console.Write("Enter a number: ");
string entry = System.Console.ReadLine();
if (entry == "quit") {
System.Console.WriteLine("Goodbye!");
} else {
int entryToInt = int.Parse(entry);
int squaredEntry = entryToInt * entryToInt;
System.Console.WriteLine(entry + " multiplied by itself is iqual to " + squaredEntry + ". It means that the Square Root of " + squaredEntry + " is " + entry + ".");
}
}
}
}
}
1 Answer
Emmanuel C
10,636 PointsBy not setting keepGoing to false, you've made an infinite loop. Even if the user enters "quit", the writeline will print "Goodbye!" but youll end up back at the beginning of the while loop. You may want to set keepGoing to false there, that way the program actually ends when the users enter quit. Also if you want to invert the if..else you can write
if(entry != "quit")
Then the you can have your main logic and the else can have the goodbye.
brevans26
15,198 Pointsbrevans26
15,198 PointsIt makes sense! Thank you!