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#

Ionut Cristian Vinte
Ionut Cristian Vinte
1,780 Points

Write a program that prompts the user to enter the number of times to enter “Yay!” then print “Yay!”that amount of times

I`m sure this is pretty easy, but I would be glad to get some help with it and understand it. "Prompts the user to enter the number of times to enter “Yay!”. I've done this for you. Leave this as it is. Prints “Yay!” to the screen that number of times (Hint: You’ll need a loop for this. You’ll also need to keep track of how many times the loop has run)."

5 Answers

Yosmar Hernandez
Yosmar Hernandez
2,556 Points

Here is my Try!

using System;

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

        for(int i=0;i<input;i++)
           {
            Console.WriteLine("\"Yay!\"");
           }

    }
}

}

Wilner Laforest
PLUS
Wilner Laforest
Courses Plus Student 478 Points

System.Console.Write("Enter number of times to enter Yay!"); var numOfTimes = System.Console.ReadLIne(); //in the ReadLine 'I' needs to be simple int num = int.Parse(numOfTimes); int val=0; while(true){ if (val<num){ val++ System.Console.WriteLine("At location" +val+ " Yay" );} }

But we didn't learn for loop yet. I used this: Console.Write("Enter the number of times to print \"Yay!\": "); int repeat=int.Parse(Console.ReadLine()); int counter=0; while(true) { if(counter<=repeat) { Console.WriteLine("Yay!"); counter+=1; } else { Console.ReadLine(); } } But it's said: "Code is too long for lunch."

Mazen Halawi
Mazen Halawi
7,806 Points

I dont have a console to try it but that should work. you might need to enter exception handlers and restrictions if you like, but below is the basic for it to run.

System.Console.Write("Enter number of times to enter Yay!");
var numOfTimes = System.Console.ReadLIne();
int num = int.Parse(numOfTimes);
for (int i=0; i<num; i++) {
     System.Console.WriteLine("\"Yay!\"");
}
Antonio De Rose
Antonio De Rose
20,885 Points

Hi Mazen,

Your code was helpful, just to refactor into it, I'd say, one change, if people are copying and pasting directly from your code

System.Console.Write("Enter number of times to enter Yay!");
var numOfTimes = System.Console.ReadLIne(); //in the ReadLine 'I' needs to be simple
int num = int.Parse(numOfTimes);
for (int i=0; i<num; i++) {
     System.Console.WriteLine("\"Yay!\"");
}