【发布时间】:2011-12-17 17:34:16
【问题描述】:
Java 中的关键字break 可用于跳出循环或switch 语句。有什么东西可以用来打破方法吗?
【问题讨论】:
-
这个问题可能也值得检查:stackoverflow.com/q/18188123/2182237
Java 中的关键字break 可用于跳出循环或switch 语句。有什么东西可以用来打破方法吗?
【问题讨论】:
使用return 关键字退出方法。
public void someMethod() {
//... a bunch of code ...
if (someCondition()) {
return;
}
//... otherwise do the following...
}
来自我上面链接的 Java 教程:
任何声明为 void 的方法都不会返回值。它不需要包含 return 语句,但它可以这样做。在这种情况下,可以使用 return 语句从控制流块中分支出来并退出方法,并且可以像这样简单地使用:
return;
【讨论】:
void 方法返回。如果您的方法需要返回一个值,那么它将类似于return array;,其中array 是您在方法中创建的。
要添加到其他答案,您还可以通过手动抛出异常来退出方法:
throw new Exception();
【讨论】:
如何在java中突围??
Ans:最佳方式:System.exit(0);
Java 语言提供了三种跳转语句,可以中断正常的程序流程。
这些包括 break 、continue 、return 、带标签的break语句 例如
import java.util.Scanner;
class demo
{
public static void main(String args[])
{
outerLoop://Label
for(int i=1;i<=10;i++)
{
for(int j=1;j<=i;j++)
{
for(int k=1;k<=j;k++)
{
System.out.print(k+"\t");
break outerLoop;
}
System.out.println();
}
System.out.println();
}
}
}
输出:1
现在请注意以下程序:
import java.util.Scanner;
class demo
{
public static void main(String args[])
{
for(int i=1;i<=10;i++)
{
for(int j=1;j<=i;j++)
{
for(int k=1;k<=j;k++)
{
System.out.print(k+"\t");
break ;
}
}
System.out.println();
}
}
}
输出:
1
11
111
1111
and so on upto
1111111111
同样,您可以使用 continue 语句,只需在上面的示例中将 break 替换为 continue。
要记住的事情:
案例标签不能包含涉及变量或方法调用的运行时表达式
outerLoop:
Scanner s1=new Scanner(System.in);
int ans=s1.nextInt();
// Error s1 cannot be resolved
【讨论】:
如果您在递归方法中深入递归,则抛出和捕获异常可能是一种选择。
与只返回上一级的 Return 不同,异常会从递归方法中跳出,并进入最初调用它的代码中,在那里它可以被捕获。
【讨论】:
使用return 退出方法。
public void someMethod() {
//... a bunch of code ...
if (someCondition()) {
return;
}
//... otherwise do the following...
}
这是另一个例子
int price = quantity * 5;
if (hasCream) {
price=price + 1;
}
if (haschocolat) {
price=price + 2;
}
return price;
【讨论】:
if (true) return; 是我使用的最佳解决方案。您可以使用判断为真或假的 if(condition) 测试。
为什么?
return;:给出Error:(105, 9) java: unreachable statement
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World 1st code ");
return;
System.out.println("Hello World 2nd code ");
}
}
由于以下错误,编译失败。
Main.java:14: error: unreachable statement
System.out.println("Hello World 2nd code ");
^
1 error
您可以使用以下方式在线测试它:https://www.onlinegdb.com/online_java_compiler
exit(0); :如您所知,终止所有程序(java.lang.System.exit() 方法终止当前正在运行的 Java 虚拟机。@987654322 @); 所以 exit ,不允许只中断方法并继续执行调用者
您可以使用以下代码测试这 3 种技术:
public class Main
{
public static void do_something(int i)
{
System.out.println(" i : "+i);
// break the method
/// System.exit(0); // stop all the program
/// return; // Main.java:20: error: unreachable statemen
if(true) return;
// do some computing
int res = i*i;
System.out.println(" res : "+res);
}
public static void main(String[] args) {
for (int i = 0; i <5; i++)
{
do_something(i);
}
System.out.println("Ouiiiii , work finished ");
}
}
结果:
i : 0
i : 1
i : 2
i : 3
i : 4
Ouiiiii , work finished
【讨论】:
使用return 关键字退出方法。
public void someMethod() {
//... a bunch of code ...
if (someCondition()) {
return;
}
//... otherwise do the following...
}
请注意: 我们可以使用 break 语句来中断/退出循环,而不是整个程序。
退出程序:System.exit()方法:System.exit有状态码,表示终止,如:
exit(0) :表示成功终止。
exit(1) 或 exit(-1) 或任何非零值 - 表示终止不成功。
【讨论】: