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 trialAlbert González
22,953 PointsNo enclosing loop ¿? What is wrong?
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
var counter = 0;
Console.Write("Enter the number of times to print \"Yay!\": ");
var input = Console.ReadLine();
try
{
var times = int.Parse(input);
while (counter < times)
{
Console.Write("Yay!");
counter += 1;
}
}
catch (FormatException)
{
Console.WriteLine("you must enter a whole number");
}
catch (ArgumentNullException)
{
Console.WriteLine("You must enter a positive number");
continue;
}
Console.ReadLine();
}
}
}
2 Answers
Kristian Gausel
14,661 PointsYou are close but you need to change your check for negative numbers to something like this:
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
var counter = 0;
Console.Write("Enter the number of times to print \"Yay!\": ");
var input = Console.ReadLine();
try
{
var times = int.Parse(input);
if(times < 0){
Console.WriteLine("You must enter a positive number");
} else {
while (counter < times)
{
Console.Write("Yay!");
counter += 1;
}
}
}
catch (FormatException)
{
Console.WriteLine("you must enter a whole number");
}
Console.ReadLine();
}
}
}
Allan Blain
2,418 PointsI had this problem after adding the Console.ReadLine(); it still failed but afer copying and pasting the whole thing exactly it passed
Albert González
22,953 PointsAlbert González
22,953 PointsOh, I don't saw it! Thank you very much for your help!
Kristian Gausel
14,661 PointsKristian Gausel
14,661 PointsIf this answer is satisfactory, please mark it as the best answer so we can close the thread =)