栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

1.Java常用API——Math&System类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

1.Java常用API——Math&System类

1.Math 1.Math类概述

Math包含执行基本数字运算的方法

没有构造方法如何使用类中的成员:看类的成员是否都是静态的,如果是,通过类名就可以直接调用

方法名说明
public static int abs(int a)返回参数的绝对值
public static double ceil(double a)返回大于或等于参数的最小double值,等于一个整数
public static double floor(double a)返回小于或等于参数的最大double值,等于一个整数
public static int round(float a )按照四舍五入返回最接近参数的int
public static int max(int a,int b)返回两个int值中的较大值
public static int min(int a,int b)返回两个int值中的较小值
public static double pow(double a,double b)返回a的b次幂的值
public static double random()返回值为double的正值,[0.0,1.0)
package study01;

public class mathDemo {
    public static void main(String[] args) {

//① public static int abs(int a)                 | 返回参数的绝对值
        System.out.println(Math.abs(88));
        System.out.println(Math.abs(-88));
        System.out.println("=========================");
//② public static double ceil(double a)          | 返回大于或等于参数的最小double值,等于一个整数
        System.out.println(Math.ceil(9.99));
        System.out.println(Math.ceil(9));
        System.out.println("=========================");
//③ public static double floor(double a)         | 返回小于或等于参数的最大double值,等于一个整数
        System.out.println(Math.floor(9.99));
        System.out.println(Math.floor(9));
        System.out.println("==========================");
//④ public static int round(float a )            | 按照四舍五入返回最接近参数的int
        System.out.println(Math.round(5.4));
        System.out.println(Math.round(5.5));
        System.out.println("==========================");
//⑤ public static int max(int a,int b)           | 返回两个int值中的较大值
        System.out.println(Math.max(15,20));
        System.out.println(Math.max(20,15));
        System.out.println("===========================");
//⑥ public static int min(int a,int b)           | 返回两个int值中的较小值
        System.out.println(Math.min(20,15));
        System.out.println(Math.min(15,20));
        System.out.println("===========================");
//⑦ public static double pow(double a,double b) | 返回a的b次幂的值
        System.out.println(Math.pow(10,2));
        System.out.println(Math.pow(2,10));
        System.out.println("============================");
//⑧ public static double random()                | 返回值为double的正值,[0.0,1.0)
        System.out.println(Math.random());
        System.out.println((int)(Math.random()*100));

    }
}
2.System类
  1. System类的常用方法
方法名说明
public static void exit(int status)终止当前运行的java虚拟机,非零表示异常终止
public static long currentTimeMillis()返回当前时间(以毫秒为单位)
  1. System.exit()示例代码
package study01;

public class systemDemo {
    public static void main(String[] args) {
        int a = 0;
        for (int i = 0;i<100;i++) {
            a++;
//            System.out.println(a);//从1输出到11
            if (a>10){
                System.exit(0);
            }
//            System.out.println(a);//从1输出到10
        }
//        System.out.println(a);//不输出,因为在循环结束前整个Java虚拟机终止了,不会执行到该语句
    }
}
  1. System.currentTimeMillis()示例代码
package study01;

public class systemDemo2 {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 0;i < 10000;i++){
            System.out.println(i);
        }
        System.out.println("=================");
        long end = System.currentTimeMillis();
        System.out.print("共耗时:");
        System.out.print(end-start);
        System.out.println("毫秒");
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/285574.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号