”并非所有代码路径都会返回值”当返回枚举类型时
我有枚举列表和方法,但收到错误:“并非所有代码路径都返回值”
我的方法有什么问题吗?我确信我总是返回 STANY 类型:/
感谢帮助:)
private enum STANY { PATROL, CHAT, EAT, SEARCH, DIE };
private STANY giveState(int id, List<Ludek> gracze, List<int> plansza)
{
// Sprawdz czy gracz stoi na polu z jedzeniem i nie ma 2000 jednostek jedzenia
bool onTheFood = false;
onTheFood = CzyPoleZjedzeniem(id, gracze, plansza, onTheFood);
if (onTheFood && (gracze[id].IloscJedzenia < startFood / 2))
return STANY.EAT;
// Sprawdz czy gracz nie stoi na polu z innym graczem
bool allKnowledge = true;
allKnowledge = CzyPoleZInnymGraczem(id, gracze, allKnowledge);
if (!allKnowledge)
return STANY.CHAT;
// Jesli ma ponad i rowna ilosc jedzenia patroluj
if (gracze[id].IloscJedzenia >= startFood / 2)
return STANY.PATROL;
// Jesli ma mniej niz polowe jedzenia szukaj jedzenia
if (gracze[id].IloscJedzenia > 0 && gracze[id].IloscJedzenia < startFood / 2)
return STANY.SEARCH;
// Jesli nie ma jedzenia umieraj
if (gracze[id].IloscJedzenia <= 0)
return STANY.DIE;
}
I have enum list and method and i get error: " not all code paths return a value"
Some idea whats wrong in my method ? I am sure I always return STANY type :/
Thanks for help :)
private enum STANY { PATROL, CHAT, EAT, SEARCH, DIE };
private STANY giveState(int id, List<Ludek> gracze, List<int> plansza)
{
// Sprawdz czy gracz stoi na polu z jedzeniem i nie ma 2000 jednostek jedzenia
bool onTheFood = false;
onTheFood = CzyPoleZjedzeniem(id, gracze, plansza, onTheFood);
if (onTheFood && (gracze[id].IloscJedzenia < startFood / 2))
return STANY.EAT;
// Sprawdz czy gracz nie stoi na polu z innym graczem
bool allKnowledge = true;
allKnowledge = CzyPoleZInnymGraczem(id, gracze, allKnowledge);
if (!allKnowledge)
return STANY.CHAT;
// Jesli ma ponad i rowna ilosc jedzenia patroluj
if (gracze[id].IloscJedzenia >= startFood / 2)
return STANY.PATROL;
// Jesli ma mniej niz polowe jedzenia szukaj jedzenia
if (gracze[id].IloscJedzenia > 0 && gracze[id].IloscJedzenia < startFood / 2)
return STANY.SEARCH;
// Jesli nie ma jedzenia umieraj
if (gracze[id].IloscJedzenia <= 0)
return STANY.DIE;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果这些条件都不满足,则不会返回。您需要使用
if...elseif...else
或者在所有 if 语句之后有一个 return,如果没有返回任何内容(没有满足任何 if 条件),则将返回一个值。
there's no return if none of those if conditions are met. You need to either use
if...elseif...else
or have a return after all of the if statements that will return a value if nothing has been returned (none of the if conditions were met).
也许您确定总是会给出返回类型,但编译器不会[想象所有的 if 条件都失败 - 即:当您的代码由外部程序执行时变量被更改。那么会发生什么?]
只需在底部粘贴一个 return 作为“默认”值即可。如果你愿意的话,你也可以抛出异常,因为永远不应该到达底部,对吧?
Maybe you're sure that a return type will always be given, but the compiler isn't [imagine that all the if conditions fail - ie: a variable was changed while your code was executing by an external program. Then what would happen?]
Just stick a return at the bottom as a "default" value. You could also throw an exception too, if you want, since the bottom should never be reached, right?
这与您返回 ENUM 的事实无关,而与编译器检测到您从未使用任何值调用 RETURN 的情况有关。
您应该在函数底部添加一个 return 语句来返回一些默认值。
This has nothing to do with the fact that you're returning an ENUM, and everything to do with the fact that the compiler detects that there are cases where you never call RETURN with any value.
You should add a return statement at the bottom of your function that returns some default value.
这是因为每个
return
语句前面都有一个if
语句。因此,如果所有if
语句都为 false,则不会返回任何值。如果不可能所有if
语句都为 false,请删除if (gracze[id].IloscJedzenia <= 0)
,因为它是多余的。如果不是,请在末尾添加return null;
或其他内容,或者抛出错误。This is because every
return
statement is preceded by anif
statement. Therefore, if all of yourif
statements are false, no value will be returned. If it's impossible for allif
statements to be false, removeif (gracze[id].IloscJedzenia <= 0)
, as it's redundant. If not, add areturn null;
or something at the end, or throw an error.替换
为
:将注释中的多余行保留为文档。
Replace this:
With this:
Leave the redundant line in the comment as documentation.
编译器的静态分析(即旨在检查此条件的代码)并不总是 100% 准确。但是,当它不是 100% 准确时,编译器往往会错误地给出误报(即,表示它不会从所有路径返回),而不是误报(即,不 说它不会从所有路径返回)。
因此在底部添加一个 return,使用一系列“if .. else if ... else”语句来代替,或者抛出异常来表明条件“不可能”。
The compiler's static analysis (i.e. the code that is designed to check for this condition) is not always 100% accurate. But when it's not 100% accurate, the compiler will tend to err on the side of giving a false positive (that is, saying it doesn't return from all paths) than a false negative (that is, not saying it doesn't return from all paths).
So add a return at the bottom, use a series of "if .. else if ... else" statements instead, or throw an exception to indicate that the condition is "impossible".