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 Final error

using System;

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

            int numTimes =0;

            try {
                   numTimes = int.Parse(Console.ReadLine());

            }

            catch(FormatException)
            {
                Console.WriteLine("You must enter a whole number.");
            }

            finally
            {
                if(numTimes <= 0)
                {
                    Console.WriteLine("You must enter a positive number.");
                }
            }


            int counter =0;
            while(counter != numTimes)
            {
                Console.WriteLine("Yay!");
                counter += 1;
            }

        }
    }
}

why am getting Bummer! Try again!

nvm i ran the code in visual studio and it was infinite loop when i entered negative number

1 Answer

Steven Parker
Steven Parker
231,261 Points

You might want to make sure the output loop only runs when the input value is valid.

Also, you may not want to use the "finally" clause because it runs even if the exception occurs.