表示程序正常退出

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. java使用.net的webservice

    1.下载最新的axis2 http://mirrors.hust.edu.cn/apache//axis/axis2/java/core/1.6.3/axis2-1.6.3-bin.zip 2.解压使 ...

  2. ubuntu下修改ip重启系统ip不变

    今天同学问我ubuntu下ip如何写死,我想起这周在公司我们队长也问过我,我就在这把我实验的方法说一下. 打开终端: sudo vim /etc/network/interfaces 然后按如下修改: ...

  3. Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) D. Little Artem and Dance

    题目链接: http://codeforces.com/contest/669/problem/D 题意: 给你一个初始序列:1,2,3,...,n. 现在有两种操作: 1.循环左移,循环右移. 2. ...

  4. 给定一颗二叉搜索树,请找出其中的第k小的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。

    // ConsoleApplication2.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "stdafx.h ...

  5. 母函数入门笔记(施工中…

    定义:对于一个数列,它的母函数(即生成函数)为   为了对这个准确求值,我们设    举一个简单的例子 例1 对于数列 他的生成函数为 ,那么应用一下等比数列求和公式 这里由于 所以当时 那么   例 ...

  6. 阿里云ubuntu12.04下安装使用mongodb

    阿里云ubuntu12.04下安装mongodb   apt-get install mongodb 阿里云ubuntu12.04下卸载mongodb,同时删除配置文件     apt-get pur ...

  7. 【WCF--初入江湖】目录

    [WCF--初入江湖]目录 [WCF--初入江湖]01 WCF编程概述 [WCF--初入江湖]02 WCF契约 [WCF--初入江湖]03 配置服务 [WCF--初入江湖]04 WCF通信模式 [WC ...

  8. Splay树再学习

    队友最近可能在学Splay,然后让我敲下HDU1754的题,其实是很裸的一个线段树,不过用下Splay也无妨,他说他双旋超时,单旋过了,所以我就敲来看下.但是之前写的那个Splay越发的觉得不能看,所 ...

  9. Openfire 代码部署报错: Variable references non-existent resource:${workspace_loc:openfire_src}

    Variable references non-existent resource:${workspace_loc:openfire_src} -DopenfireHome=“${workspace_ ...

  10. lintcode 中等题:和大于S的最小子数组

    题目 和大于S的最小子数组 给定一个由 n 个整数组成的数组和一个正整数 s ,请找出该数组中满足其和 ≥ s 的最小长度子数组.如果无解,则返回 -1. 样例 给定数组 [2,3,1,2,4,3]  ...