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

Something wrong with my code

Treehouse keep saying "Try again!". There no errors in my code.

Someone who knows what i did wrong?

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            var entry = Console.ReadLine();
            try
            {
                var times = int.Parse(entry);
                int i = 0;
                while (true)
                {
                    i += 1;
                    Console.WriteLine("Yay!");
                    if (i == times)
                    {
                        break;
                    }
                }   
            }
            catch (FormatException)
            {
                Console.WriteLine("You must enter a whole number.");
            }
        }
    }
}

2 Answers

Henrique Vignon
PLUS
Henrique Vignon
Courses Plus Student 6,415 Points

Hmmm, indeed there's nothing wrong with it, it would work just fine, but perhaps the challenge is actually expecting you to use a for loop? (in fact that would be the best loop to use in this situation, even though using while works too)

Henrique Vignon
Henrique Vignon
Courses Plus Student 6,415 Points

I just checked the challenge and passed using for, just to be sure you're still in step 2 right? because step 3 asks for something you haven't done in your code, to check if the number entered is negative or not.

Thanks for the response. I had to make a for loop.