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

C# Basics Final Challenge

I am struggling with this challenge. I am able to get "Yay" to print x amount of times based on user input, but I am getting hung up in regards to input that is 0 or less, as well as string input that is not "Yay".

I have tried if/else statements as well as try/catch, but I keep ending up in infinite loops (which I can't exit out of), or the program stops printing multiples of "Yay" and just prints it once.

To make this simpler, I slimmed my code down to what works so far. Basically I am looking for a nudge in the right direction.

Thanks!

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string input = Console.ReadLine();
            int times = int.Parse(input);
            while(times > 0)
            {
                Console.WriteLine("Yay!");
                times--;
            }

        }
    }
}
Rahul Gupta
Rahul Gupta
667 Points

Try this.

using System;

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

            int Number = int.Parse(Console.ReadLine());


            while (Number >0 ) 
            {
                Console.WriteLine("Yay");
                Number--;
            }

        }
    }
}
Steven Parker
Steven Parker
231,248 Points

Try putting in the code for the next task, and then we can help you with what's causing it not to work.

1 Answer

steven simpson
steven simpson
11,644 Points

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() { Console.Write("Enter the number of times to print \"Yay!\": "); string input = Console.ReadLine(); int times = int.Parse(input); while(times > 0 && input == "Yay!") { Console.WriteLine("Yay!"); times--; }

    }
}

}

Steven Parker
Steven Parker
231,248 Points

:x: Did you try that in the challenge? The input is a number so it will never be equal to "Yay!".