表示程序正常退出

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)的更多相关文章

  1. android Activity类中的finish()、onDestory()和System.exit(0) 三者的区别

    android Activity类中的finish().onDestory()和System.exit(0) 三者的区别 Activity.finish() Call this when your a ...

  2. System.exit(0)和System.exit(1)区别

    System.exit(0)是将你的整个虚拟机里的内容都停掉了 ,而dispose()只是关闭这个窗口,但是并没有停止整个application exit() .无论如何,内存都释放了!也就是说连JV ...

  3. System.exit(0)作用

    System.exit(0)作用   public class HelloGoodbye{ try{ System.out.println(“Hello World”); System.exit(0) ...

  4. android开发时,finish()跟System.exit(0)的区别

      这两天在弄Android,遇到一个问题:所开发的小游戏中有背景音乐,玩的过程中始终有音乐在放着,然后在我退出游戏后,音乐还在播放! 我看了一下我最开始写的退出游戏的代码,就是简单的finish() ...

  5. System.exit(0)和System.exit(1)区别:

    System.exit(0)是将你的整个虚拟机里的内容都停掉了,而finish()只是退出了activity,并没有退出应用,Application还是存在于内存中的,除非被系统回收.无论如何,内存都 ...

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

  7. android Process.killProcess 和 System.exit(0) 区别

    1 Process.killProcess  和 System.exit(0) 两个都会 kill 掉当前进程. 你可以打开 DDMS 查看进程号,或 adb shell 进入 shell 然后 ps ...

  8. system.exit(0) vs system.exit(1)

    2.解析 查看java.lang.System的源代码,我们可以找到System.exit(status)这个方法的说明,代码如下: /** * Terminates the currently ru ...

  9. android中 System.exit(0)的理解

    public class HelloGoodbye{ try{ System.out.println(“Hello World”); System.exit(0); } finally { Syste ...

随机推荐

  1. Xcode8兼容iOS7手记-b

    对于Xcode8的发布,苹果也是来了个大的跳跃,默认最低支持的iOS版本为8.0,当然也并不是说8.0以下就直接放弃了,虽然表现出来的是这样,毕竟使用8.0以下系统的还是大有人在的,老项目要兼容iOS ...

  2. 《C++Primer》复习——with C++11 [1]

    1.头文件中不应包含using声明,因为头文件的内容会拷贝到所有引用到他的文件中去,如果头文件里有谋个using声明,那么每个使用了该头文件的文件就会有这个声明,由于不经意间包含了一些名字,反而可能产 ...

  3. 自己的一些 Demo,源码链接

    1.指纹解锁(GitHub). 2.JS 与 OC 交互(GitHub). 3.模仿 HTML 下拉菜单(GitHub). 4.OC开发常用类目(GitHub).

  4. ios 框架学习笔记

    ios主要的系统层次: 一.Cocoa Touch 层:创建应用程序主要使用的框架. 1.关键技术: AirDrop:实现应用间通信. Text Kit:处理文本和排版. UIKit Dynamics ...

  5. 【BZOJ】【2588】COT(Count On a Tree)

    可持久化线段树 maya……树么……转化成序列……所以就写了个树链剖分……然后每个点保存的是从它到根的可持久化线段树. 然后就像序列一样查询……注意是多个左端点和多个右端点,处理方法类似BZOJ 19 ...

  6. java Socket用法详解(转)

    在客户/服务器通信模式中, 客户端需要主动创建与服务器连接的 Socket(套接字), 服务器端收到了客户端的连接请求, 也会创建与客户连接的 Socket. Socket可看做是通信连接两端的收发器 ...

  7. windows server 2008 R2 远程连接用户数修改

    设置windows server 2008 R2 远程连接用户数修改,三步搞定 1.运行(win+R)中输入tsconfig.msc 2.双击“限制每个用户只能进行一个会话”,取消这个选项负选框 3. ...

  8. PHP输出中文乱码的问题

    用echo输出的中文显示成乱码, 其实应该是各种服务器脚本都会遇到这个问题, 根本还是编码问题, 一般来说出于编码兼容考虑大多的页面都将页面字符集定义为utf-8 <meta http-equi ...

  9. httpclient 人人网

    登录的站点是3g.renren.com 因为是手机人人, 页面比较简单 首先用HttpGet取出"http://3g.renren.com"的html代码, 是用Jsoup解析出登 ...

  10. 空格的URL编码

    Q: 为什么我看的教材一会说是“+” 一会说是“%20” A: urlencode(" ") '返回+encodeURI(" ") '返回%20是有区别的