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 trialandy march
341 PointsC# Basics Final Challenge
I am struggling with this challenge. I am able to get "Yay" to print x amount of times based on user input, but I am getting hung up in regards to input that is 0 or less, as well as string input that is not "Yay".
I have tried if/else statements as well as try/catch, but I keep ending up in infinite loops (which I can't exit out of), or the program stops printing multiples of "Yay" and just prints it once.
To make this simpler, I slimmed my code down to what works so far. Basically I am looking for a nudge in the right direction.
Thanks!
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
Console.Write("Enter the number of times to print \"Yay!\": ");
string input = Console.ReadLine();
int times = int.Parse(input);
while(times > 0)
{
Console.WriteLine("Yay!");
times--;
}
}
}
}
Steven Parker
231,236 PointsTry putting in the code for the next task, and then we can help you with what's causing it not to work.
1 Answer
steven simpson
11,644 Pointsusing System;
namespace Treehouse.CodeChallenges { class Program { static void Main() { Console.Write("Enter the number of times to print \"Yay!\": "); string input = Console.ReadLine(); int times = int.Parse(input); while(times > 0 && input == "Yay!") { Console.WriteLine("Yay!"); times--; }
}
}
}
Steven Parker
231,236 PointsDid you try that in the challenge? The input is a number so it will never be equal to "Yay!".
Rahul Gupta
667 PointsRahul Gupta
667 PointsTry this.