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

A. D.
A. D.
4,192 Points

I don't know how to do this.

I can't figure out how to do this. I don't understand why I need a loop to tell the computer to print out the word Yay! a specified number of times if this doesn't repeat or accumulate.

Program.cs
using System;

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

        }
    }
}

4 Answers

Steven Parker
Steven Parker
231,236 Points

The reason you need a loop is the "specified number" is not known at compile time.

Your program will receive a number from the user indicating how many times to print. The loop will then use a conditional test to determine if the desired number has been printed, and until that number is reached it will repeat the print.

A. D.
A. D.
4,192 Points

Okay, that sheds a little light. I suppose I also have to test for bad input, like less than or equal to zero and nonsensical entries. I'm working on it. Thanks for the answer.

Steven Parker
Steven Parker
231,236 Points

Always go by the challenge instructions. Do only what is asked as any extra logic may cause your answer not to pass.

If I recall this particular challenge, it has multiple tasks and later ones will involve some specific error checking.

A. D.
A. D.
4,192 Points

I ended up using this, and it worked, but I don't think this is what the instructor was after. Can you, Steven, or anyone post an alternative answer that might be more elegant?


/* using System;

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

      while(yayTimes > 0)
      {
        Console.Write("Yay!");
        yayTimes--;
      }
    }

}

} */

Steven Parker
Steven Parker
231,236 Points

Though it's hard to read without proper formatting, it appears you did a good job with the code. :+1:

A. D.
A. D.
4,192 Points

Sorry, not sure why the above code snippet posted the way it did. :/

Steven Parker
Steven Parker
231,236 Points

Instructions for formatting can be found in the Markdown Cheatsheet below the "Add an Answer" area. :arrow_heading_down: