c#返回字符串的方法?

时间:2013-12-27 15:19:42

标签: c# loops

此方法应该有一个循环并返回一个字符串。我怎么做?这就是我到目前为止所拥有的。我是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();
    }
}

++我尝试了你建议的所有内容,但我认为我应该重新定义该方法应该返回一个由main打印的字符串。

6 个答案:

答案 0 :(得分:6)

我假设您需要返回循环中构造的字符串(而不仅仅是其他答案中的一些任意字符串)。你需要构建一个字符串而不是只写出字符串,然后返回该字符串:

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();
}

请注意,我还修改了循环的第二行,以消除冗余的String.Format参数。

答案 1 :(得分:3)

如果你想返回一个字符串,而不仅仅是添加一个return语句

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";
}

答案 2 :(得分:2)

您可以使用return关键字:

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";
}

答案 3 :(得分:1)

使用StringBuilder在循环中构建字符串,然后返回其字符串值。

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();
}

答案 4 :(得分:1)

不知道你实际要求的是什么,但如果你应该将整首歌作为一个大字符串返回,那么就这样做:

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();
}

希望这有助于......祝你好运!

答案 5 :(得分:0)

这只是一个最简单问题的另一个答案。循环后使用'return'语句。

    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;
    }