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#

This final code challenge will require you to use almost everything we've covered in this course.

This final code challenge will require you to use almost everything we've covered in this course. You’ll write a program to celebrate completing this course by printing “Yay!” to the screen a given number of times. Write a program that: Prompts the user to enter the number of times to enter “Yay!”. I've done this for you. Leave this as it is. Prints “Yay!” to the screen that number of times (Hint: You’ll need a loop for this. You’ll also need to keep track of how many times the loop has run). You can print them all on the same line or on separate lines.

my code:

using System;

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

7 Answers

Steven Parker
Steven Parker
231,124 Points

:point_right: You have to initialize a variable when you declare it with "var".

I'm wondering if you're a JavaScript programmer. Writing "var i;" would be fine there, but not in C#.

But you can combine the declaration with the initialization inside the for loop:

            for (var i = 0; i < entry; i += 1)

You may notice that I also changed the comparison from less-than-or-equal "<=" to just less-than "<". Otherwise, you get one more loop than intended.

yes I already learned js and I'm more comfortable with, also because I learned scripts children of js as gml and ags, I code for games, and I'm trying to learn C# to use Unity 3D so I will finish everything here about C# and I going to move in game development

anyway thank you very much, it's working :)

it was the operator

Icke Code
Icke Code
550 Points

My code

using System;

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

            try {             
                for (int i = 0; i < entry; i++){
                Console.Write("Yay!");
                }
            }
            catch(FormatException)
            {
                Console.Write("Exception");
            }
        }
    }
}

Hello Could you please explain me how you get the value of var entry I think it's from the line above but it doesn't make sense to me how you connect that value to entry? And what doese i++ mean in the code?

Canaan Longworth
PLUS
Canaan Longworth
Courses Plus Student 1,910 Points

using System;

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

        for(var i = 0; i < entry; i++){
            Console.WriteLine("Yay!");

        }          

    }
}

}

using System;

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

            try {             
                for (int i = 0; i < entry; i++){
                Console.Write("Yay!");
                }
            }
            catch(FormatException)
            {
                Console.Write("Exception");
            }
        }
    }
}
HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,058 Points

using System;

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

        try {     
            int entry = int.Parse(Console.ReadLine());  
            for (int i = 0; i < entry; i++){
            Console.Write("Yay!");
            }
        }
        catch(FormatException)
        {
            Console.Write("You must enter a whole number.");
        }
    }
}

}

HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,058 Points

using System;

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

        try {     
            int entry = int.Parse(Console.ReadLine());  
            for (int i = 0; i < entry; i++){
            Console.Write("Yay!");
            }
        }
        catch(FormatException)
        {
            Console.Write("You must enter a whole number.");
        }
    }
}

}

HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,058 Points

```using System;

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

        try {     
            int entry = int.Parse(Console.ReadLine());  
            for (int i = 0; i < entry; i++){
            Console.Write("Yay!");
            }
        }
        catch(FormatException)
        {
            Console.Write("You must enter a whole number.");
        }
    }
}

}```