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

Luis Santos
Luis Santos
3,566 Points

I'm on the last task and I can't seem to get the validation sequence correct. Help will be really appreciated.

What am I missing here? I tried to define the negative number validation question the problem asks and nothing seems to work. I am really having a hard time with this last question.

Program.cs
using System;

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

          var numOfTimes = System.Console.ReadLine();



          try
          {

          int num = int.Parse(numOfTimes);

          for (int i=0; i<num; i++) {

          System.Console.WriteLine("\"Yay!\"");
          }

          }
          catch (FormatException)
          {

           System.Console.WriteLine("You must enter a whole number.");

          }
        }
    }
}

1 Answer

anil rahman
anil rahman
7,786 Points
using System;

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

          var numOfTimes = System.Console.ReadLine();


                try
                {
                  int num = int.Parse(numOfTimes);

                  if(num < 0)
                  {
                     System.Console.WriteLine("You must enter a positive number.");
                  }

                  for (int i=0; i<num; i++) 
                  {
                     System.Console.WriteLine("\"Yay!\"");
                  }

                }
                catch (FormatException)
                {

                 System.Console.WriteLine("You must enter a whole number.");

                }


        }
    }
}