哪个方法用于终止之间的Java程序的执行? [英] Which method is used to terminate the execution of Java program in between?

查看:421
本文介绍了哪个方法用于终止之间的Java程序的执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在C ++中使用exit()之类的函数异常终止程序的执行,我们可以在Java中使用该函数。
例如: - 在下面的程序中,我想在第一次打印i的值时立即终止执行。

We used functions like exit() in C++ to abnormally terminate the execution of program, which function can we use in Java. For ex :- In following program I want to terminate the execution as soon the value of i is printed for 1st time.

//following is the program :  
class Lcm {
        public static void main( String args[] ) {

            int a = Integer.parseInt( args[0] );
            int b = Integer.parseInt( args[1] );

            for ( int i=1 ; i<=a*b ; i++ ) {
                if ( i%a==0 && i%b==0 ) {
                    System.out.println( "lcm is: " + i );
                    /* i want to terminate the program
                     * when the value of i is printed
                     * for very 1st time
                     */
                }
            }
        }
    }
}


推荐答案

System.exit(0) Runtime.getRuntime()。exit(0)

这篇关于哪个方法用于终止之间的Java程序的执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆