表示程序正常退出

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. learning from the previous teams

    开发人员水平有限.分配任务的时候经常有说这个事儿做不到,或者压根不知道怎么做:验收工作频出意外,DEV写了一个模块之后,验收的时候发现模块质量不行,代码质量低是其次,无法按照给定的接口工作.设计不足. ...

  2. MITK Tutorial (三)

    Step 2: Use the template with the plugins to read a image 在exampleplugin插件中QmitkAwesomeView.cpp中添加头文 ...

  3. Jquery 固定悬浮层以及固定表头

    /* =========================================================== * jquery.autofix_anything.js v1 * === ...

  4. 剑指offer--面试题19

    题目:求二叉树镜像 根据作者思路,自己所写代码如下: void BinaryTreeMirror(BinaryTreeNode* pRoot) { if(pRoot == NULL) return; ...

  5. NHibernate 基础

    install-package nhibernate install-package nunit Customer.cs public class Customer { public virtual ...

  6. 【机器学习】BP神经网络实现手写数字识别

    最近用python写了一个实现手写数字识别的BP神经网络,BP的推导到处都是,但是一动手才知道,会理论推导跟实现它是两回事.关于BP神经网络的实现网上有一些代码,可惜或多或少都有各种问题,在下手写了一 ...

  7. MongoDB 性能优化五个简单步骤

    MongoDB 一直是最流行的 NoSQL,而根据 DB-Engines Ranking 最新的排行,时下 MongoDB 已经击败 PostgreSQL 跃居数据库总排行的第四位,仅次于 Oracl ...

  8. java基础知识回顾之---java String final类普通方法的应用之“按照字节截取字符串”

    /*需求:在java中,字符串“abcd”与字符串“ab你好”的长度是一样,都是四个字符.但对应的字节数不同,一个汉字占两个字节.定义一个方法,按照最大的字节数来取子串.如:对于“ab你好”,如果取三 ...

  9. no such partition grub rescue>

    事出有因: 电脑系统是win7+ubuntu,然后在win7下把ubuntu的分区给删除了,重启,出现 no such partition grub rescue> 错误. 原因是双系统之前是由 ...

  10. lintcode : 空格替换

    题目: 空格替换 设计一种方法,将一个字符串中的所有空格替换成 %20 .你可以假设该字符串有足够的空间来加入新的字符,且你得到的是“真实的”字符长度. 样例 对于字符串"Mr John S ...