java 它向我抛出错误不兼容的类型:意外的返回值——返回 10;请更正它请注意我在哪里做错了代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31152403/
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
it throws me error incompatible types: unexpected return value -- return 10; please correct it make note where i am doing wrong code?
提问by RAHUL kumar
//return rule break
//返回规则中断
class Fianalblck
{
public static void main(String... s)
{
//int show
int x;
{
try {
return 10;
}
catch(Exception e) {
}
finally {
return 20;
}
} System.out.println(x);
}
}
回答by UnknownOctopus
Your main method is void
meaning it can't return anything. If you wish to return a value, create another method that is a returnable type (boolean, int, double, String,
etc.) then call that method from your main.
你的主要方法void
意味着它不能返回任何东西。如果您希望返回一个值,请创建另一个可返回类型的方法(boolean, int, double, String,
等),然后从您的 main 调用该方法。
It looks like you need to start from the beginning, hereis a link to a java basics tutorial.
看起来你需要从头开始,这里是一个java基础教程的链接。
Hereis a tutorial on return
.
这里有一个教程return
。
回答by KDP
void means the method cannot return any value. if you wish to quit the application with exit code you can use System.exit().
void 表示该方法不能返回任何值。如果您希望使用退出代码退出应用程序,您可以使用 System.exit()。
public static void main(String[] args) {
System.exit(10);
}
回答by Brian
I guess this is what you want to do:
我想这就是你想要做的:
class Fianalblck
{
private int func() {
try {
return 10;
}
catch(Exception e) {
}
finally {
return 20;
}
}
public static void main(String... s)
{
Fianalblck f = new Fianalblck();
int x;
x = f.func();
System.out.println(x);
}
}
You want to try whether 10 or 20 will be returned from a method.
您想尝试从方法中返回 10 还是 20。
In java, you have to declare a new method, and call that method with the instance of Fianalblck.
在 java 中,您必须声明一个新方法,并使用 Fianalblck 的实例调用该方法。
回答by apm
Main method cannot return a valuedirectly.
Main 方法不能直接返回值。
Error: Main method must return a value of type void in class first, please
define the main method as:
public static void main(String[] args)
But you can simply use returnin a main method.
但是您可以简单地在 main 方法中使用return。
public static void main(String[] args){
...
...
...
return;
}