• java常用API(Math,System,Runtime)


    Math

    Math的常用方法

    在这里插入图片描述

    package myMath;
    
    public class myMath {
        public static void main(String[] args) {
            System.out.println(Math.abs(-99));//这个表示取绝对值的 打印结果为99
    
            //但是他有个bug.int最大取值范围是-2,147,483,648 到2,147,483,647
            System.out.println(Math.abs(-2147483648));//打印结果还是-2,147,483,648
            //解决以上bug可以用
            /*Math.absExact(-2147483648);*/
    
    
            System.out.println(Math.ceil(12.3));//打印结果13 向上取整
    
    
            System.out.println(Math.floor(12.3));//向下取整 也称去尾法 打印结果12
    
            System.out.println(Math.round(12.3));//四舍五入法
    
            System.out.println(Math.max(20, 30));//获取最大值
            System.out.println(Math.min(20, 30));//获取最大值
    
    
            System.out.println(Math.pow(2, 3));
            //如果第二个参数是0到1之间的小数 表示开根号
            System.out.println(Math.pow(4, 0.5));//打印结果为2
    
            System.out.println(Math.pow(2, -2));//打印结果为0.25  因为他表示2的-2次方
    
    
            System.out.println(Math.sqrt(4));//打印结果为2  开平方根
            System.out.println(Math.cbrt(8));//2  开立方根
    
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    总结

    在这里插入图片描述

    判断质数

    质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数 只能被1和本身整除的数

    在这里插入图片描述

    由此得出 求出平方跟 只需要判断左边红圈内容即可

    package myMath;
    
    public class isPrime {
        public static void main(String[] args) {
    
            System.out.println(IsPrime(14));
    
        }
        public static boolean IsPrime(int number){
    
            for (int i = 2; i <= Math.sqrt(number); i++) {
                if(number%i==0){
                    return false;
                }
            }
            return true;
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    自幂数(水仙花数)

    在这里插入图片描述

    水仙花数是三位数 所以一定在100到999之间

    package myMath;
    
    public class zimishu {
        public static void main(String[] args) {
            //统计水仙花数
            int count=0;
    
            for (int i = 100; i <= 999; i++) {
                //得到100到999每一位数字
    
                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(sum);
                }
            }
            System.out.println(count);
    
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    System

    在这里插入图片描述

    因为是静态修饰的方法所以可以直接用类名调用

    package myMath;
    
    public class apiSystem157 {
        public static void main(String[] args) {
    
    
            //0表示虚拟机正常停止
            //1表示虚拟机一场停止
            //System.exit(0);
    
    
            long l = System.currentTimeMillis();
            System.out.println(l);//返回从java诞生时间(1970年1月1 8:00)到系统当前时间的毫秒数
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    System.currentTimeMillis()应用场景如下

    package myMath;
    
    public class isPrime {
        public static void main(String[] args) {
    
            long start=System.currentTimeMillis();//开始时间
            for (int i = 1; i <= 100000; i++) {
                boolean flag=IsPrime(i);
                if(flag){
                   // System.out.println(i);
                }
            }
            long end=System.currentTimeMillis();
            System.out.println(end - start);//判断1000以内有几个质数 用了多长时间
    
    
        }
        public static boolean IsPrime(int number){
    
            for (int i = 2; i <= Math.sqrt(number); i++) {
                if(number%i==0){
                    return false;
                }
            }
            return true;
    
        }
    }
    
    
    
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    在这里插入图片描述

    还有些注意事项 如下

    在这里插入图片描述

    总结

    在这里插入图片描述

    Runtime

    他表示当前虚拟机的运行环境,他不是静态,想调用得先获取对象
    在这里插入图片描述

    package myRuntime;
    
    import java.io.IOException;
    
    public class myRunime {
        public static void main(String[] args) {
            System.out.println(Runtime.getRuntime());//查看当前系统运行环境
    
            System.out.println(Runtime.getRuntime().availableProcessors());//获取cpu线程数
    
            System.out.println(Runtime.getRuntime().maxMemory()/1024/1024);//jvm从系统获得总内存大小byte
    
            System.out.println(Runtime.getRuntime().totalMemory());//jvm从系统中已经获得的内存
    
            System.out.println(Runtime.getRuntime().freeMemory());//剩余内存
    
            try {
                System.out.println(Runtime.getRuntime().exec("shutdown -s -t 10000"));//运行cmd命令 这个代码表示电脑 10000毫秒后取消 输入-a取消
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    高并发项目-分布式Session解决方案
    Linux ____02、Linux开关机、目录介绍、文件目录相关命令(常用命令)
    Qt 学习(二) —— Qt工程基本文件详解
    MySQl表的增删查改(CRUD)
    java基础面试
    springboot+学校运动会信息管理 毕业设计-附源码231058
    Python之爬虫之BeautifulSoup学习
    【Python实战】--词云制作
    短信发送:使用RestTemplate的时候,遇到类型无法转换的问题
    头文件和编译的问题
  • 原文地址:https://blog.csdn.net/nie2459010516/article/details/133350662