Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回字符串的c#方法?_C#_Loops - Fatal编程技术网

返回字符串的c#方法?

返回字符串的c#方法?,c#,loops,C#,Loops,这个方法应该有一个循环并返回一个字符串。我该怎么做?这就是我目前所拥有的。我是C#的新手 ++我尝试了您建议的所有方法,但我认为应该重新表述该方法应该返回一个由main打印的字符串。如果要返回字符串,而不仅仅是添加一个return语句 public string BLoop() { for (int i = 99; i > 0; i--) { Console.WriteLine(string.Format("{0} bottles o

这个方法应该有一个循环并返回一个字符串。我该怎么做?这就是我目前所拥有的。我是C#的新手


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

如果要返回字符串,而不仅仅是添加一个
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";
}

您可以使用
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";
}

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

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();
}
请注意,我还修改了循环的第二行,以消除多余的
字符串。Format
参数。

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

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

不知道你到底想要什么,但是如果你想把整首歌作为一个大字符串返回,那么就这样做:

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

希望这有帮助…祝你好运

这只是一个最简单问题的另一个答案。在循环后使用“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;
    }

请首先……这是c#and development的基础。请在要求我们做家庭作业或做任何你正在做的事情之前先学一点。我尝试了你建议的所有方法,但我认为我应该重新表述这个方法,它应该返回一个由main打印的字符串。-这正是我的答案。我在这篇帖子上窃笑。不错,而且它确实回答了这个问题^
    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;
    }