JavaSE 15 Math 数学工具类

第二十章 Math 数学工具类

20.1 概述

  • java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算的相关操作。

20.2 绝对值

public static double abs(double num)

20.3 向上取整(向正方向)

public static double ceil(double num)

20.3 向下取整

public static double floor(double num)

20.4 四舍五入

public static long round(double num)

20.5 Math.PI

  • 代表近似的圆周率常量(double)。
    20.6 练习
/*
计算-10.8到5.9之间,绝对值大于6或者小于2.1的整数有多少个?
 */
public class DemoMath {
    
    

    public static void main(String[] args) {
    
    
        double min = -10.8;
        double max = 5.9;

        int count = 0;
        for (int i = (int) min; i < max; i++) {
    
    
            int result = Math.abs(i);
            if (result > 6 || result < 2.1) {
    
    
                System.out.print(i + " ");//-10 -9 -8 -7 -2 -1 0 1 2
                count++;
            }
        }

        System.out.println();
        System.out.println("满足条件的数量是:" + count);//9
    }

}

猜你喜欢

转载自blog.csdn.net/Niiuu/article/details/104232721