Java if / for / while 中的“缺少 return 语句”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23058029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Missing return statement" within if / for / while
提问by user3531560
I have a question regarding return statements used within if()
while()
or for()
statements.
As you can see in the following method, it is expecting that I return
a String value.
The problem is that if I were to use a return
statement within my if
statement block, the compiler would return the error missing return statement
.
我有一个关于在if()
while()
或for()
语句中使用的返回语句的问题。正如您在下面的方法中看到的,它期望 Ireturn
是一个字符串值。问题是,如果我return
在if
语句块中使用语句,编译器会返回错误missing return statement
。
public String myMethod()
{
if(condition)
{
return x;
}
}
Of course I could change the method header to void
and use System.out.println
instead of return
. But is this the right way to do it? am i missing something?
当然,我可以改变的方法头void
,并使用System.out.println
替代return
。但这是正确的做法吗?我错过了什么吗?
Any help is highly appreciated.
任何帮助都受到高度赞赏。
采纳答案by Bhushan Kawadkar
If you put return statement in if
, while
or for
statement then it may or may not return value. If it will not go inside these statement then also that method should return some value ( that could be null). To ensure that, compiler will force you to write this return statement which is after if
, while
or for
.
如果您将 return 语句放入if
, while
orfor
语句中,则它可能会或可能不会返回值。如果它不会进入这些语句,那么该方法也应该返回一些值(可能为空)。为确保这一点,编译器将强制您在if
,while
或之后编写此 return 语句for
。
But if you write if
/ else
block and each one of them is having return in it then compiler knows that either if
or else
will get execute and method will return a value. So this time compiler will not force you.
但是,如果你写if
/else
块,并将它们中的每一个具有它的回报则编译器知道,要么if
或else
将得到执行和方法会返回一个值。所以这次编译器不会强迫你。
if(condition)
{
return;
}
else
{
return;
}
回答by Guillermo Merino
That's because the function needsto return a value. Imagine what happens if you execute myMethod()
and it doesn't go into if(condition)
what would your function returns? The compiler needs to know what to return in every possible execution of your function
那是因为该函数需要返回一个值。想象一下,如果您执行myMethod()
并且它没有进入if(condition)
您的函数返回的内容,会发生什么?编译器需要知道在每次可能的函数执行中返回什么
Checking Java documentation:
检查 Java 文档:
Definition: If a method declaration has a return type then there must be a return statement at the end of the method. If the return statement is not there the missing return statement error is thrown.
This error is also thrown if the method does not have a return type and has not been declared using void (i.e., it was mistakenly omitted).
定义:如果一个方法声明有一个返回类型,那么在方法的末尾必须有一个 return 语句。如果 return 语句不存在,则抛出缺少的 return 语句错误。
如果该方法没有返回类型并且没有使用 void 声明(即,它被错误地省略),也会抛出此错误。
You can do to solve your problem:
您可以执行以下操作来解决您的问题:
public String myMethod()
{
String result = null;
if(condition)
{
result = x;
}
return result;
}
回答by JavaLearner
This will return the string only ifthe condition is true.
仅当条件为真时才会返回字符串。
public String myMethod()
{
if(condition)
{
return x;
}
else
return "";
}
回答by bNd
Try with, as if if condition
returns false, so it will return empty otherwise nothing to return.
尝试使用,好像if condition
返回false,因此它将返回空否则返回任何内容。
public String myMethod()
{
if(condition)
{
return x;
}
return ""
}
Because the compiler doesn't know if any of those if blocks will ever be reached, so it's giving you an error.
因为编译器不知道是否会到达这些 if 块中的任何一个,所以它会给你一个错误。
回答by CodeCamper
That is illegal syntax. It is notan optionalthing for you to return a variable. You MUST return a variable of the type you specify in your method.
那是非法语法。这是不是一个可选的东西你回来一个变量。您必须返回您在方法中指定的类型的变量。
public String myMethod()
{
if(condition)
{
return x;
}
}
You are effectively saying, I promiseany class can use this method(public) and I promise it will always return a String(String).
您实际上是在说,我保证任何类都可以使用此方法(公共),并且我保证它将始终返回一个字符串(字符串)。
Then you are saying IF my condition is true I will return x. Well that is too bad, there is no IF in your promise. You promised that myMethod will ALWAYS return a String. Even if your condition is ALWAYS true the compiler has to assume that there is a possibility of it being false. Therefore you always need to put a return at the end of your non-void method outside of any conditions JUST IN CASE all of your conditions fail.
那么你是说如果我的条件为真,我将返回 x。好吧,那太糟糕了,您的承诺中没有 IF。您承诺 myMethod 将始终返回一个字符串。即使您的条件始终为真,编译器也必须假设它有可能为假。因此,您总是需要在任何条件之外的非 void 方法的末尾放置一个 return ,以防万一您的所有条件都失败。
public String myMethod()
{
if(condition)
{
return x;
}
return ""; //or whatever the default behavior will be if all of your conditions fail to return.
}
回答by d3vnico
You have to add a return statement if the condition
is false.
如果condition
为假,则必须添加 return 语句。
public String myMethod() {
if(condition) {
return x;
}
// if condition is false you HAVE TO return a string
// you could return a string, a empty string or null
return otherCondition;
}
FYI:
供参考:
回答by loknath
Any how myMethod() should return a String value .what if your condition is false is myMethod return anything? ans is no so you need to define return null or some string value in false condition
myMethod() 应该如何返回一个字符串值。如果条件为假,myMethod 会返回什么?ans 为否,因此您需要在 false 条件下定义返回 null 或某个字符串值
public String myMethod() {
boolean c=true;
if (conditions) {
return "d";
}
return null;//or some other string value
}