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 trialryota kurazono
Courses Plus Student 4,248 PointsI don't know what's wrong with my code.
The code is below. If someone know about this mistake, please let me know. Thank you!
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
int i = 0 ;
int num;
Console.Write("Enter the number of times to print \"Yay!\": ");
num = Convert.ToInt32(Console.ReadLine());
while (i <= num)
{
Console.WriteLine("Yay!");
}
}
}
}
1 Answer
Raffael Dettling
32,999 PointsYou forgot to decrement your num variable by 1 each time the loops run at the current state you got an loop which runs infinitely. Also you can declare and assign the variable num in one line of code to safe space ^^ And in the while statment use num > 0 instead of >= you want to stop at 0 :)
static void Main()
{
Console.Write("Enter the number of times to print \"Yay!\": ");
int i = Convert.ToInt32(Console.ReadLine());
while(i > 0){
i--;
Console.Write("Yay!");
}
}