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

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,558 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