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

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!");

             }
        }
    }
}