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

Andrew Clough
Andrew Clough
2,557 Points

repeat string

I'm trying to repeat a string for this challenge. I've tried using different loops but i cant find a good order it put it in. and when i looked on-line, i found solutions that would not work. the repeating Sequence would in the comment section.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            while (true)
            {
                Console.WriteLine("Enter the number of times to print \"Yay!\": ");
                String entry = Console.ReadLine();

                if (entry.ToLower() == "quit")
                {
                    Console.WriteLine("Thank you for using all the \"Yays!\"");
                    break;
                }
                else if (entry.ToLower () == "stop")
                {
                    Console.WriteLine("Thank you for using all the \"Yays!\"");
                    break;
                }

                try
                {
                    /*int numbersOfYays = int.Parse(entry);
                    String yay = "\"Yay!\"";
                    int y = int.Parse(yay);
                    int z = 0;
                    do
                    {
                        Console.WriteLine(yay);

                    }
                    while (z != numbersOfYays);
                    continue;*/
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid entry");
                    continue;
                }
            }

        }
    }
}

1 Answer

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

The code challenge does not seem to ask for a WHILE loop wrapping around the rest of the method body. Have you tried deleting the outer most WHILE loop and the IF-ELSE statement that breaks out of the WHILE loop?

In addition, why are you doing this?

String yay = "\"Yay!\"";
int y = int.Parse(yay);

You don't need to escape the String and you can also just hard code the string into the WriteLine function like so: Console.WriteLine("Yay!");. Also, the y variable is not used and "Yay!" won't parse into a number because it's not in the right format.

Try making those changes and let me know where you stand.

Andrew Clough
Andrew Clough
2,557 Points

The challenge is asking to repeat "Yay!" as many times as the user desires. I turned the string yay in to a int because I thought a solution that involved a an int would work. Even with the changes, I still would need a solution to repeat the string. The WHILE loop is there so the code can run again when the user is done with there previous request.