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 trialMichael Thompson
7,427 PointsDon't know where to go from here. Final challenge problem.
I'm struggling trying to figure out how to get it to print "Yay!" the amount of times the user inputs.
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
Console.Write("Enter the number of times to print \"Yay!\": ");
var a = Console.ReadLine();
try
{
var b = double.Parse(a);
for(b=1; b<10; b++)
{
if (b > 0)
{
Console.Write("Yay!");
}
else {
Console.Write("That is not a valid input."); continue;
}
}
}
catch(FormatException)
{
Console.Write("That is not a valid format.");
}
}
}
}
1 Answer
Steven Parker
231,210 PointsThe "for" loop replaces what you converted from the console input and always repeats 9 times.
To repeat the correct number of times, you can create a new loop variable to compare with "b", or you could skip the initialization and decrement "b" until it reaches 0.
Michael Thompson
7,427 PointsMichael Thompson
7,427 PointsI'm still lost.
Steven Parker
231,210 PointsSteven Parker
231,210 PointsI've commented the lines involved:
My suggestions would involve replacing the loop line. To create a new loop variable to compare with "b":
for (int n=0; n < b; n++)
or you could skip the initialization and decrement "b" until it reaches 0:
for (/* no initialization */; b > 0; b--)
But I have to wonder ... wasn't this part of the code working when you passed task 1?
Michael Thompson
7,427 PointsMichael Thompson
7,427 PointsThere was a task 1?
Steven Parker
231,210 PointsSteven Parker
231,210 PointsTask 1 is the basic loop. I assumed you had passed that because it's not until task 2 that the instructions ask you to detect and respond to parse errors.
If you're still on task 1, none of that code should be there.
Michael Thompson
7,427 PointsMichael Thompson
7,427 PointsI wrote more than I needed for the first task. Didn't fail it but wasn't necessary till part 2. I got to task 3 and now I'm hung up on it. Thanks for your help.