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 ...
随机推荐
- LintCode-Hash Function
In data structure Hash, hash function is used to convert a string(or any other type) into an integer ...
- MITK Tutorial (三)
Step 2: Use the template with the plugins to read a image 在exampleplugin插件中QmitkAwesomeView.cpp中添加头文 ...
- Android journey3 @点击事件的4种写法
对于android布局中的控件,如Button等会有相应的点击事件去响应它所需要的功能,今天我们就以电话拨号器的代码说明下几种点击事件: package com.itheima.phone; impo ...
- c++ 从标注输入流读取行
#include <string.h> #include <iostream> #include <vector> #include <stdio.h> ...
- Ext学习-基础概念,核心思想介绍
1.目标 本阶段的目标是通过学习一些基础知识来对EXTJS有个整体的了解,知道EXTJS的基础语法,核心设计思想等等 2.内容 1.基础部分学习 2.EXTJS类系统介绍 3.EXTJ ...
- 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
// test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- GhostDoc:生成.NET API文档的工具 (帮忙文档)
在 Sandcastle:生成.NET API文档的工具 (帮忙文档) 后提供另一个生成API文档的工具. 1) 准备工作 安装GhostDoc Proc. 收费的哦.... 这个工具的优势是不像 ...
- NS记录
NS(Name Server)记录是域名服务器记录,用来指定该域名由哪个DNS服务器来进行解析. 1名词简介 您注册域名时,总有默认的DNS服务器,每个注册的域名都是由一个DNS域名服务器来进行解析的 ...
- 将LINUX变成路由器
将LINUX变成路由器 2009-06-04 22:38:45 标签:LINUX 路由器 休闲 职场 版权声明:原创作品,如需转载,请与作者联系.否则将追究法律责任. LINUX系统是一个强大 ...
- Web前端业界氛围极好的群——鬼懿IT
鬼群简介 鬼懿IT主群号:,鬼懿IT-成长群:181368696 , 创建于2005年12月 ,聚集的业内人事包括:阿当,大漠,辣妈,崔凯,Rei,周裕波,司徒正美,丸子,鬼森林,寒冬,franky, ...