如何在Java中中断或退出方法?

如何在Java中中断或退出方法?,java,break,Java,Break,Java中的关键字break可用于中断循环或switch语句。有什么可以用来中断方法吗?使用关键字退出方法 public void someMethod() { //... a bunch of code ... if (someCondition()) { return; } //... otherwise do the following... } public void someMethod() { //... a bun

Java中的关键字
break
可用于中断循环或switch语句。有什么可以用来中断方法吗?

使用关键字退出方法

public void someMethod() {
    //... a bunch of code ...
    if (someCondition()) {
        return;
    }
    //... otherwise do the following...
}
 public void someMethod() {
        //... a bunch of code ...
        if (someCondition()) {
            return;
        }
        //... otherwise do the following...
    }
public void someMethod() {
    //... a bunch of code ...
    if (someCondition()) {
        return;
    }
    //... otherwise do the following...
}
从我上面链接的Java教程中:

任何声明为void的方法都不会返回值。它不需要包含return语句,但可以这样做。在这种情况下,可以使用return语句从控制流块中分支并退出该方法,其使用方式如下:

return;

如何在java中突围

Ans:最佳方式:
System.exit(0)

Java语言提供了三种跳转语句,允许您中断正常的程序流

其中包括中断继续返回标记中断语句 例如

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
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

要添加到其他答案中,还可以通过手动抛出异常退出方法:

throw new Exception();

如果您在递归方法中深入研究递归,则抛出和捕获异常可能是一种选择


与只返回一级的Return不同,异常会从递归方法中分离出来,并进入最初调用它的代码中,在那里可以捕获它。

使用
Return
退出方法

public void someMethod() {
    //... a bunch of code ...
    if (someCondition()) {
        return;
    }
    //... otherwise do the following...
}
 public void someMethod() {
        //... a bunch of code ...
        if (someCondition()) {
            return;
        }
        //... otherwise do the following...
    }
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;
使用关键字退出方法

public void someMethod() {
    //... a bunch of code ...
    if (someCondition()) {
        return;
    }
    //... otherwise do the following...
}
 public void someMethod() {
        //... a bunch of code ...
        if (someCondition()) {
            return;
        }
        //... otherwise do the following...
    }
public void someMethod() {
    //... a bunch of code ...
    if (someCondition()) {
        return;
    }
    //... otherwise do the following...
}
请注意: 我们可以使用break语句,这些语句仅用于中断/退出循环,而不是整个程序

要退出程序:System.exit()方法:
System.exit
具有状态代码,用于说明终止,例如:
退出(0):表示成功终止。
退出(1)或退出(-1)或任何非零值–表示终止不成功。

如果(真)返回是我使用的最佳解决方案。您可以使用if(条件)测试来给出true或false

为什么?

  • 使用
    返回单独:给出错误:(105,9)java:unreachable语句
公共类主
{
公共静态void main(字符串[]args){
System.out.println(“你好世界第一代码”);
返回;
System.out.println(“Hello World第二代码”);
}
}
由于以下错误,编译失败

您可以使用以下工具进行在线测试:

  • 使用退出(int状态)示例
    退出(0):如您所知,终止所有程序(java.lang.System.exit()方法终止当前运行的java虚拟机)So exit,不允许仅中断方法并继续执行调用方
您可以使用以下代码测试这3种技术:

公共类主
{
公共静态void do_something(int i)
{
System.out.println(“i:+i”);
//破门而入
///System.exit(0);//停止所有程序
///return;//Main.java:20:错误:无法访问的状态
如果(真)返回;
//做一些计算
int res=i*i;
System.out.println(“res:+res”);
}
公共静态void main(字符串[]args){

对于(int i=0;i),这个问题可能也值得检查:退出构造函数是否有效?我知道这有点奇怪,但我需要这个技巧。