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 trial

C# C# Basics (Retired) Perfect Final

no understanding of question

I have written this code:

but it say " Try again "

I want to know where is my mistake

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
 class Program
    {
        static void Main()
        { try
            {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string input = Console.ReadLine();   

                int counter = 0;
                int times = int.Parse(input);
                while (true)
                {
                    counter += 1;
                    Console.WriteLine("Yay!");
                    if (counter == times)
                    {
                        break;
                    }
                }
                Console.WriteLine(counter);
            }
            catch (FormatException)
            {
                Console.WriteLine("You must enter a whole number.");

            }

        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,269 Points

The way the loop is constructed, it will always output one time before testing the count. So imagine if the number entered was 0. When you re-arrange how the testing is done, you might want to consider placing the test in the conditional part of the while statement itself.

Also, the instructions don't ask for the count to be printed, that could cause trouble with the validation.