Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perfect Final

any one please help

!

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
                    while (true) // if the condition is true the code runs 
        {
            int counter = 0;
            int result;

            Console.Write("Enter the number of times to print \"Yay!\": ");


            try // This one catches Validation error 
            {
                result = Convert.ToInt32(Console.ReadLine());
                if (result <= counter)// put at first try method becuase the condition needs to be checked before proceeding 
                {
                    Console.WriteLine("You must enter a positive number.");
                    return;

                }
                while (result > counter)
                {

                    Console.WriteLine("Yay!");

                    counter++;
                  //added the if to tell message about neededing positive number 
                }


            }
            catch (FormatException) //catches the exception error 
            {
                Console.WriteLine("You must enter a whole number.");
                break;


            }

            Console.ReadLine();
        }
    }

}

1 Answer

Hamse,

You need to get an input from the user and save it to a variable(int n). Then create a for or while loop that prints "Yay" n times.

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
             int counter = Console.ReadLine();
             int i;
             for(i=0;i<counter;i++){
                     Console.Write("Yay!");

             }
        }
    }
}