JAVA学习第十三章——常用API(一)

Math

        //int取值范围 : -2147483648 ~ 2147483647
        int a = Math.abs(-88);
        System.out.println(a);
        //System.out.println(Math.absExact(-2147483648));

        //向上取整(正无穷方向)
        System.out.println(Math.ceil(12.34));
        System.out.println(Math.ceil(-12.34));
        System.out.println("----------------------------------");

        //向下取整(负无穷方向)
        System.out.println(Math.floor(12.34));
        System.out.println(Math.floor(-12.34));
        System.out.println("----------------------------------");

        //四舍五入
        System.out.println(Math.round(12.34));
        System.out.println(Math.round(-12.34));
        System.out.println("----------------------------------");

        //获取较大值
        System.out.println(Math.max(12,30));
        System.out.println("----------------------------------");

        //获取较小值
        System.out.println(Math.min(12,30));
        System.out.println("----------------------------------");

        //返回a的b次幂
        System.out.println(Math.pow(2,3));
        System.out.println("----------------------------------");

        System.out.println(Math.sqrt(4)); //开平方
        System.out.println(Math.cbrt(8)); //开立方
        System.out.println("----------------------------------");

        //获取随机数
        for (int i = 0; i < 4; i++) {
            System.out.println(Math.random());
        }
        System.out.println("----------------------------------");

        System.out.println(Math.floor(Math.random() * 100) + 1);
        //Math.random() [0.0 1.0)
        // * 100 [0.0 100.0)
        //Math.floor() 去掉小数
        // + 1 [1 100.0]
方法名说明
public static int abs(int a)获取参数绝对值
public static double ceil(double a)向上取整
public static double floor(double a)向下取整
public static int round(float a)四舍五入
public static int max(int a,int b)获取两个int值中的较大值
public static double pow(double a,double b)返回a的b次幂的值
public static double randow()返回值为double的随机值,范围[0.0,1.0)

Math练习(一)

判断一个数是否为质数

        //判断一个数是否为质数
        System.out.println(isPrime(997));

    }

    public static boolean isPrime(int number){
        int count = 0;
        for (int i = 2; i <= Math.sqrt(number); i++) {
            count ++;
            if(number % i == 0){
                return false;
            }
        }
        System.out.println(count);
        return true;
    }

Math练习(二)

自幂数,一个n位自然数等于自身各个数位上数字的n次幂之和

        //统计一共有多少个水仙花数

        int count = 0;
        for (int i = 100; i <= 999; i++) {
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100 % 10;

            double sum = Math.pow(ge,3) + Math.pow(shi,3) + Math.pow(bai,3);

            if(sum == i){
                count++;
            }
        }

        System.out.println(count);
    //统计一共有多少个水仙花数
    public static void main(String[] args) {
        //证明没有两位的自幂数
        double sum = 0;

        if(live(sum)){
            System.out.println("存在");
        }else {
            System.out.println("不存在");
        }


    }

    public static boolean live(double sum){
        for (int i = 100; i <= 999; i++) {
            int ge = i % 10;
            int shi = i / 10 % 10;

            sum = Math.pow(ge,3) + Math.pow(shi,3);

            if(sum == i){
                return true;
            }
        }

        return false;
    }
    //证明没有两位的自幂数
        //统计一共有多少个四叶玫瑰数

        int count = 0;
        for (int i = 1000; i <= 9999; i++) {
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100 % 10;
            int qian = i / 1000 % 10;

            double sum = Math.pow(ge,4) + Math.pow(shi,4) + Math.pow(bai,4) + Math.pow(qian,4);

            if(sum == i){
                count++;
            }
        }

        System.out.println(count);
    //统计一共有多少个四叶玫瑰数
        //统计一共有多少个五角星数

        int count = 0;
        for (int i = 10000; i <= 99999; i++) {
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100 % 10;
            int qian = i / 1000 % 10;
            int wan = i / 10000 % 10;

            double sum = Math.pow(ge,5) + Math.pow(shi,5) + Math.pow(bai,5) + Math.pow(qian,5) + Math.pow(wan,5);

            if(sum == i){
                count++;
            }
        }

        System.out.println(count);
    //统计一共有多少个五角星数

System

        //0:表示当前虚拟机正常停止
        //非0:表示当前虚拟机异常停止
        //System.exit(0);

        //long l = System.currentTimeMillis();
        //System.out.println(l); //毫秒

        //拷贝数组
        int[] arr1 = {1,2,3,4,5,6,7};
        int[] arr2 = new int[7];

        System.arraycopy(arr1,0,arr2,0,7);
        //把arr1数组中的数据拷贝到arr2中
        //参数一:数据源,要拷贝的数据从哪来
        //参数二:从数据源数组中的第几个索引开始拷贝
        //参数三:目的地,拷贝到哪去
        //参数四:目的地数组的索引
        //参数五:拷贝的个数

        for (int i = 0; i < arr2.length; i++) {
            int a = arr2[i];
            System.out.print(a);
        }
方法名说明
public static void exit(int status)终止当前运行的Java虚拟机
public static long currentTimeMillis()返回当前系统的时间毫秒值形式
public static void arraycopy(数据源数组,起始索引,目的地数组,起始索引,拷贝个数)数组拷贝

Runtime

    public static void main(String[] args) throws IOException {

/*        Runtime r1 = Runtime.getRuntime();
        Runtime r2 = Runtime.getRuntime();
        System.out.println(r1 == r2);*/

//        Runtime.getRuntime().exit(0);

//        System.out.println(Runtime.getRuntime().availableProcessors());

//        System.out.println(Runtime.getRuntime().maxMemory() / 1024 / 1024);

//        System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024);

//        System.out.println(Runtime.getRuntime().freeMemory() / 1024 / 1024);

        Runtime.getRuntime().exec("notepad");

    }
方法名说明
public static Runtime getRuntime()当前系统的运行环境对象
public void exit(int status)停止虚拟机
public int availableProcessors()获得CPU的线程数
public long maxMemory()JVM能从系统中获取总内存大小(单位byte)
public long totalMemory()JVM已经从系统中获取总内存大小(单位byte)
public long freeMemory()JVM剩余内存大小(单位byte)
public Process exec(String command)运行cmd命令

Tips

以上学习内容均来自于B站黑马程序员

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值