早上,简单的愚蠢的问题。我发现有类似问题的帖子,但通读后并不能解决我的错误。返回值从循环内不显示
Can't get a return value from a foreach loop inside a method
的方法:METH1 meth2 ECT ....所有返回一个值,但此刻我正在错误
“错误1“Proj5.Program.meth1 (int)':不是所有的代码路径都会为每个方法返回一个值“。
我的逻辑推测是它没有看到循环内的值? ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Proj5
{
class Program
{
static void Main()
{
for (int i = 1; i < 101; i++)
{
if (i == 3 || 0 == (i % 3) || 0 == (i % 5) || i == 5)
{
Console.Write(meth1(i));
Console.Write(meth2(i));
Console.Write(meth3(i));
Console.Write(meth4(i));
}
else
{
Console.Write(TheInt(i));
}
}
Console.ReadLine();
}
static string meth1(int i)
{
string f = "fuzz";
if (i == 3)
{
return f;
}
}
static string meth2(int i)
{
string f = "fuzz";
if (0 == (i % 3))
{
return f;
}
}
static string meth3(int i)
{
string b = "buzz";
if (i == 5)
{
return b;
}
}
static string meth4(int i)
{
string b = "buzz";
if (0 == (i % 5))
{
return b;
}
}
static int TheInt(int i)
{
return i;
}
}
}
你只需给它一个空在它的位置?如果可能的话,我想知道背后的逻辑。 – Dan
返回'string'的方法必须*总是*返回'string'或抛出异常。你的代码不会返回任何东西,除非'if'评估为真。这解决了如果'if'评估为false则返回null。 –
听起来不错,很有道理!感谢您的小凹凸=) – Dan