115_初识Java_Math类_学习

2021/6/14 12:21:52

本文主要是介绍115_初识Java_Math类_学习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

115_初识Java_Math类_学习

  • 1、API的Math类解读
  • 2、Math类常用方法练习使用

1、API的Math类解读

  • Math类是java.lang下的类,可直接使用,无需额外导包
  • Math类是final类,不能被继承,没有子类
    在这里插入图片描述
  • Math类的构造器被private修饰,外部无法使用new创建对象
    在这里插入图片描述
  • Math类的属性和方法被static修饰,可直接通过类名.属性名类名.方法名调用
    在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

2、Math类常用方法练习使用

package com.llg.learnMath;

public class Learn1 {
    //程序的入口
    public static void main(String[] args) {
        //常用属性
        System.out.println("Math.PI = " + Math.PI);

        //常用方法
        System.out.println("随机数 Math.random() :\t" + Math.random());
        System.out.println("绝对值 Math.abs(-23) :\t" + Math.abs(-23));
        System.out.println("向上取值 Math.ceil(8.2) :\t" + Math.ceil(8.2));
        System.out.println("向下取值 Math.floor(8.9) :\t" + Math.floor(8.9));
        System.out.println("四舍五入 Math.round(2.4) :\t" + Math.round(2.4));
        System.out.println("四舍五入 Math.round(2.5) :\t" + Math.round(2.5));

        System.out.println("取大值 Math.max(2,3) :\t" + Math.max(2,3));
        System.out.println("取小值 Math.min(2,3) :\t" + Math.min(2,3));
    }
}

在这里插入图片描述

  • 静态导入方式使用 import static java.lang.Math.*;
    在这里插入图片描述


这篇关于115_初识Java_Math类_学习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程