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

I am supposed to print "Yay!" however many times the user asks me to. I'm stuck at the while loop? Please help.

How do I print a line multiple times?

2 Answers

lukaskasa
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
lukaskasa
iOS Development with Swift Techdegree Graduate 27,785 Points

you need two variables; one holding the number how many times the user asks to print 'yay!' and another variable holding the answer then

don't forget to intParse the answer.

while number > 0

print yay!

number -= 1

count += 1

Pablo Rocha
Pablo Rocha
10,142 Points

As mentioned by Lukas you will need to create a separate int variable to maintain the number of loops. Once the number of loops exceeds number of expected "Yay!"s then you need to exit the loop.

Here is the code:

number = int.Parse(entry); // parse the user input, you will need to do this inside a "try" statement so catch any exceptions

var numOfLoops = 1; // start by processing the first loop

while (numOfLoops <= number)
{
     Console.WriteLine("Yay!"); // Write one "Yay!" for this loop
     numOfLoops++; // increment by one, if <= number then the while will be complete
}