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

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
229,644 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!!