-7

This method is supposed to have a loop and return a string. How do I do that? This what I have so far. I'm new to C#.

public string BLoop()
{
    for (int i = 99; i > 0; i--)
    {
        Console.WriteLine(string.Format("{0} bottles of beer on the wall, {0}   bottles of beer.", i));
        Console.WriteLine(string.Format("Take one down, pass it around, {1} bottles of beer on the wall.", i, i - 1));
        Console.WriteLine();
    }
}

++ I tried all the thing you suggested but I think I should rephrase the method is supposed to return a string that is printed by the main.

3
  • Please learn to Google first.... Commented Dec 27, 2013 at 15:22
  • This is a basic in c# and develoment.Please Learn a little before asking us to do your homework or whatever you are doing. Commented Dec 27, 2013 at 15:23
  • I tried all the thing you suggested but I think I should rephrase the method is supposed to return a string that is printed by the main. - that's exactly what my answer gives you. Commented Dec 27, 2013 at 19:38

6 Answers 6

6

I'm assuming you need to return the string constructed in your loop (and not just some arbitrary string as in the other answers). You need to build a string instead of just writing the strings out, then return that string:

public string BLoop()
{
    var builder = new StringBuilder();
    for (int i = 99; i > 0; i--)
    {
        builder.AppendLine(string.Format("{0} bottles of beer on the wall, {0} bottles of beer.", i));
        builder.AppendLine(string.Format("Take one down, pass it around, {0} bottles of beer on the wall.", i-1));
    }

    return builder.ToString();
}

Note that I've also amended the second line of your loop to eliminate the redundant String.Format parameter.

Sign up to request clarification or add additional context in comments.

Comments

3

If you want to return a string, than just add a return statement

public string BLoop()
{
        for (int i = 99; i > 0; i--)
        {
            Console.WriteLine(string.Format("{0} bottles of beer on the wall, {0}   bottles of beer.", i));
            Console.WriteLine(string.Format("Take one down, pass it around, {1} bottles of beer on the wall.", i, i - 1));
            Console.WriteLine();
        }
    return "a string";
}

1 Comment

I snickered at this post. Nice one, besides it DOES answer the question ^.^
2

You could use the return keyword:

public string BLoop()
{
    for (int i = 99; i > 0; i--)
    {
        Console.WriteLine(string.Format("{0} bottles of beer on the wall, {0}   bottles of beer.", i));
        Console.WriteLine(string.Format("Take one down, pass it around, {1} bottles of beer on the wall.", i, i - 1));
        Console.WriteLine();
    }

    return "this is some string to return";
}

Comments

1

Use StringBuilder to build string in your loop and then return its string value.

public string BLoop()
{
    StringBuilder sb = new StringBuilder();
    for (int i = 99; i > 0; i--)
    {
        sb.AppendLine(string.Format("{0} bottles of beer on the wall, {0}   bottles of beer.", i));
        sb.AppendLine(string.Format("Take one down, pass it around, {1} bottles of beer on the wall.", i, i - 1));
        sb.AppendLine(Environment.NewLine);
    }
    return sb.ToString();
}

Comments

1

No clue what you're actually asking for, but if you're supposed to return the entire song as a big string then do this:

public string BLoop()
{
    var song = new System.Text.StringBuilder();
    for (int i = 99; i > 0; i--)
    {
        song.AppendLine(string.Format("{0} bottles of beer on the wall, {0} bottles of beer.", i));
        song.AppendLine(string.Format("Take one down, pass it around, {1} bottles of beer on the wall.", i, i - 1));
        song.AppendLine();
    }
    return song.ToString();
}

Hope this helps...good luck!

Comments

0

It's just another answer of a simplest question. Use 'return' statement after the loop.

    public string BLoop()
    {
        string sStatus = "Process Started";
        Console.WriteLine(sStatus);

        for (int i = 99; i > 0; i--)
        {
            Console.WriteLine(string.Format("{0} bottles of beer on the wall, {0}   bottles of beer.", i));
            Console.WriteLine(string.Format("Take one down, pass it around, {1} bottles of beer on the wall.", i, i - 1));
            Console.WriteLine();
        }

        sStatus = "Process Completed";
        return sStatus;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.