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

Helmuts Reinis
Helmuts Reinis
1,021 Points

Not sure why it worked, but it worked, but i think this code can be made shorter... any syggestions?

Any suggestions?

Program.cs
using System;

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


            try
            {

                var totals = int.Parse(times);
                if (int.Parse(times) <= 0)
                {
                    Console.WriteLine("You must enter a positive number.");
                }
                while (counter < totals)
                {
                    Console.Write("\"Yay\"! ");
                    counter += 1;
                }
            }

            catch (FormatException)
            {
                Console.WriteLine("you must enter a whole number.");
            }

            Console.ReadLine();

        }
    }
}

2 Answers

Steven Parker
Steven Parker
231,236 Points

:+1: You seem to have covered the requirements pretty concisely — good job.

I don't see anything spurious or redundant in your code.

One other suggestion, since you've assigned the result to a variable you don't need to call int.Parse more than once. So instead of this:

                var totals = int.Parse(times);
                if (int.Parse(times) <= 0)

You could write this:

                var totals = int.Parse(times);
                if (totals <= 0)
Helmuts Reinis
Helmuts Reinis
1,021 Points

Well, this is great... did't think it would be this simple :D