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

The code is running in VS. Why your editor not passing? It is also telling me "Task 1 fails". This wasting subscription!

Challenge task 2 of 3

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            var i = 1;  
            int z = 1;

            while(true)
            {
                try
                {              
                    Console.Write("Enter the number of times to print \"Yay!\": ");   
                    z = int.Parse(Console.ReadLine());
                    if (z < i){
                        continue;
                    }
                    break;
                }
                catch(FormatException e){
                    continue;
                }
            };

            while (i <= z){
                Console.WriteLine("Yay");
                i += 1;
            }
        }
    }
}

1 Answer

Michael Milone
Michael Milone
1,474 Points
using System;

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

            // We don't need a while loop here because we only need to prompt the user one time of how many times to print
            // yay, as in above
            /*
            var i = 1;  
            int z = 1;

            while(true)
            {
                try
                {              
                    Console.Write("Enter the number of times to print \"Yay!\": ");   
                    z = int.Parse(Console.ReadLine());
                    if (z < i){
                        continue;
                    }
                    break;
                }
                catch(FormatException e){
                    continue;
                }
            }; no semi colon here btw*/

            // we dont need a while loop here, use a for loop instead to iterate a counter and to execute code more than one 
            // time. 
            /*while (i <= z){
                Console.WriteLine("Yay");
                i += 1;
            }*/
            for (int counter = 0; counter < amountToPrintYay; counter++){
                Console.WriteLine("Yay");
            }
        }
    }
}

Thank you Michael!