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) or Runtime.getRuntime().exit(0)