System.exit(0)
表示程序正常退出
System.exit(status)
当status非0时,表示程序为非正常退出。
status=0, 关闭当前正在运行的虚拟机。
求质因数分解的程序如下:
两种算法:
一种是用System.exit(0)
// 若不加此句,代码在运行完System.out.println(number)后,会回到for中从i++开始执行,不断执行 else和后面的打印语句。是因为递归吗?
代码
package basic40;
public class Divid {
public static void divide(int a){
for (int i = 2; i <= (int)(Math.sqrt(a)); i++){
if(a % i == 0){
System.out.print(i+"*");
a = a / i;
divide(a);
break;
}
}
System.out.print(a);
System.exit(0);
}
public static void main(String []args){
int a = 90;
divide(a);
}
}
另一种是不用System.exit(0)
package basic40;
public class Divide2PrimeMultiple {
//need to judge whether the last number is prime number
public static void divide(int number) {
if (isPrimeNumber(number)) {
System.out.println(number);
} else {
int i = 2;
while(i <=(int)(Math.sqrt(number))){
if (number % i == 0) {
System.out.println(i + ",");
number = number / i;
divide(number);
break;
} else {
i++;
}
}
}
}
// }
private static boolean isPrimeNumber(int number) {
boolean flag = true;
for (int i = 2; i <= (int) (Math.sqrt(number)); i++) {
if (number % i == 0) {
flag = false;
break;
} else {
flag = true;
}
}
return flag;
}
public static void fenjie(int n){
for(int i=2;i<=(int)(Math.sqrt(n));i++){
if(n%i==0){
System.out.print(i+"*");
fenjie(n/i);
}
}
System.out.print(n);
System.exit(0);///不能少这句,否则结果会出错
}
public static void main (String []args){
int a = 90;
divide(a);
// divideNoJudge(a);
// fenjie(a);
}
}
麻烦很多,不仅有重复代码,而且增加运行负担。
有时候在递归中强制关闭编译器是有必要的。
System.exit(0)的更多相关文章
- android Activity类中的finish()、onDestory()和System.exit(0) 三者的区别
android Activity类中的finish().onDestory()和System.exit(0) 三者的区别 Activity.finish() Call this when your a ...
- System.exit(0)和System.exit(1)区别
System.exit(0)是将你的整个虚拟机里的内容都停掉了 ,而dispose()只是关闭这个窗口,但是并没有停止整个application exit() .无论如何,内存都释放了!也就是说连JV ...
- System.exit(0)作用
System.exit(0)作用 public class HelloGoodbye{ try{ System.out.println(“Hello World”); System.exit(0) ...
- android开发时,finish()跟System.exit(0)的区别
这两天在弄Android,遇到一个问题:所开发的小游戏中有背景音乐,玩的过程中始终有音乐在放着,然后在我退出游戏后,音乐还在播放! 我看了一下我最开始写的退出游戏的代码,就是简单的finish() ...
- System.exit(0)和System.exit(1)区别:
System.exit(0)是将你的整个虚拟机里的内容都停掉了,而finish()只是退出了activity,并没有退出应用,Application还是存在于内存中的,除非被系统回收.无论如何,内存都 ...
- How to use Android Activity's finish(), onDestory() and System.exit(0) methods
Activity.finish() Calling this method will let the system know that the programmer wants the current ...
- android Process.killProcess 和 System.exit(0) 区别
1 Process.killProcess 和 System.exit(0) 两个都会 kill 掉当前进程. 你可以打开 DDMS 查看进程号,或 adb shell 进入 shell 然后 ps ...
- system.exit(0) vs system.exit(1)
2.解析 查看java.lang.System的源代码,我们可以找到System.exit(status)这个方法的说明,代码如下: /** * Terminates the currently ru ...
- android中 System.exit(0)的理解
public class HelloGoodbye{ try{ System.out.println(“Hello World”); System.exit(0); } finally { Syste ...
随机推荐
- LoadRunner 学习笔记(2)VuGen运行时设置Run-Time Setting
定义:在Vugen中Run-Time Setting是用来设置脚本运行时所需要的相关选项
- 给同一个表中的两个外键写sql
一个表有两个外键都指向另一个表的主键时,如何检索他们的数据? 例如,表TableA有两个列,puserID和friendID. 表TableB有两个列,userID和userName. 我们怎样检索数 ...
- word小技巧
如何将英文和数字替换为times new romans 1,点击替换(ctrl+H) .2,在查找内容中输入”([A-Z0-9])“(不包括引号,但包括小括号),这是替换所有大写字母和数字,如果还想替 ...
- 【BZOJ】【3295】【CQOI2011】动态逆序对
树套树 Orz zyf神犇 时光倒流……逆序处理,将删点改为加点,动态维护序列. 由于是动态,要加点,所以用树状数组:同时又需要求序列中求比当前元素大/小的元素个数,所以要用平衡树. 所以方法就是在树 ...
- 20160730noip模拟赛zld
codeforces394E 如果没有在凸多边形内一点的限制,答案肯定是 如果不在凸多边形内,那么目标点肯定在凸多边形边上,我们枚举每条边,在每条边上求出距离平方和最小的点,在这些点中求出最小的 我们 ...
- Unity3D脚本中文系列教程(三)
http://dong2008hong.blog.163.com/blog/static/4696882720140302323886/ Unity3D脚本中文系列教程(二) 示,属性不被序列化或显示 ...
- SystemInfo.deviceUniqueIdentifier 返回机器码
SystemInfo.deviceUniqueIdentifier 返回机器码
- linux源代码阅读笔记 get_free_page()代码分析
/* 34 * Get physical address of first (actually last :-) free page, and mark it 35 * used. If no fre ...
- Longest Repeated Sequence【微软编程一小时-题目2】
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a sequence of integers, A = a1, a2, ... an. A c ...
- 常见的排序算法之Java代码解释
一 简要介绍 一般排序均值的是将一个已经无序的序列数据重新排列成有序的 常见的排序分为: 1 插入类排序 主要就是对于一个已经有序的序列中,插入一个新的记录.它包括:直接插入排序,折半插入排序和希尔排 ...