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

Why I always got wrong in Challenge Task 3?

In fact, I'v pasted this code to Visual Studio 2017. It can run well. But always got wrong in Challenge Task 3 of 3. It always shows that Oops! It looks like Task 2 is no longer passing!

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

                if (count < 0)
                {
                    Console.WriteLine("You must enter a positive number.");
                    continue;
                }

                int i = 1;
                while (i <= count)
                {
                    Console.Write("Yay!");
                    i++;
                }
                break;
            }
            catch (FormatException)
            {
                Console.WriteLine("You must enter a whole number.");
                continue;
            }
        }
    }
}

}

Program.cs
using System;

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

                    if (count < 0)
                    {
                        Console.WriteLine("You must enter a positive number.");
                        continue;
                    }

                    int i = 1;
                    while (i <= count)
                    {
                        Console.Write("Yay!");
                        i++;
                    }
                    break;
                }
                catch (FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");
                    continue;
                }
            }
        }
    }
}

2 Answers

Steven Parker
Steven Parker
217,533 Points

This challenge only needs one loop. You have an extra outer loop that prevents the program from ending when bad values are entered.

A program built according to the instructions will prompt and accept input one time, and based on the input it will either print "Yay!"s or an error mesage. Either way, it should end.

Thanks!! I got it!!