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 trialGem Knight
4,779 PointsCan I Please Have Some Help
I, am on the last course of C# basics, and I am unsure why this code isn't working. Can someone please help. Thank you in advance.
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
Console.Write("Enter the number of times to print \"Yay!\": ");
string reply = Console.ReadLine();
int replyAsInt = int.Parse(reply);
int counter = 0;
while (replyAsInt == replyAsInt)
{
Console.WriteLine("Yay!");
counter += 1;
}
}
}
}
2 Answers
Seth Kroger
56,413 PointsIn your while loop you are testing whether replyAsInt is equal to itself, which will always be true. The while loop will never end. What you should check for is whether counter < replyAsInt.
Gilbert Wong
7,055 PointsThe problem is in your while loop. The condition you have set will always be true which means that your loop will never end and it will be an infinite loop. You can fix this by adjusting your condition to have your counter variable less than your replyAsInt variable:
using System;
namespace Treehouse.CodeChallenges { class Program { static void Main() { Console.Write("Enter the number of times to print \"Yay!\": "); string reply = Console.ReadLine(); int replyAsInt = int.Parse(reply); int counter = 0;
while (counter < replyAsInt)
{
Console.WriteLine("Yay!");
counter += 1;
}
}
}
}
this should fix your code