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

What am I doing wrong I give up.

using System;

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

        try {
          int numberOfTimes = int.Parse(Console.ReadLine());

          if (numberOfTimes >= 0) {
            for (int i=0; i<numberOfTimes; i++) {
              Console.WriteLine("\"Yay!\"");
            } else {
              Console.Write("You must enter a positive number.");
            }
          else {}
        } catch (FormatException) {
          Console.Write("You must enter a whole number.");
        }
    }
}

}

Program.cs
using System;

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

            try {
              int numberOfTimes = int.Parse(Console.ReadLine());

              if (numberOfTimes >= 0) {
                for (int i=0; i<numberOfTimes; i++) {
                  Console.WriteLine("\"Yay!\"");
                } else {
                  Console.Write("You must enter a positive number.");
                }
              else {}
            } catch (FormatException) {
              Console.Write("You must enter a whole number.");
            }
        }
    }
}

2 Answers

andren
andren
28,526 Points
if (numberOfTimes >= 0) {
  for (int i=0; i<numberOfTimes; i++) {
    Console.WriteLine("\"Yay!\"");
  } else { // <-- You are missing a closing brace for the `if` statement
    Console.Write("You must enter a positive number.");
  }
else {} // <-- This `else` statement is redundant and not connected to any `if` statement.

If you add the closing brace, remove the redundant else and clean up the formatting a bit like this:

using System;

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

            try {
              int numberOfTimes = int.Parse(Console.ReadLine());

              if (numberOfTimes >= 0) {
                for (int i=0; i<numberOfTimes; i++) {
                  Console.WriteLine("\"Yay!\"");
                }
              } else { // <-- Added mising closing brace
                Console.Write("You must enter a positive number.");
              }
              // Removed redundant else statement

            } catch (FormatException) {
              Console.Write("You must enter a whole number.");
            }
        }
    }
}

Then your code will work.

Ok thank you so much Andren