前言:
Math类概述
Math类包含执行基本数字运算的方法
下面通过代码来演示:
//Math类的常用方法
public class MathDemo {
public static void main(String[] args) {
//public static int abs(int a) :返回参数的绝对值
System.out.println(Math.abs(66));
System.out.println(Math.abs(-66));
System.out.println("--------");
//public static double ceil(double a):返回大于或等于参数的最小double值,等于一个整数
System.out.println(Math.ceil(11.66));
System.out.println(Math.ceil(15.01));
System.out.println(Math.ceil(20));
System.out.println("--------");
//public static double floor(double a):返回小于或等于参数的最大double值,等于一个整数
System.out.println(Math.floor(20));
System.out.println(Math.floor(16.8));
System.out.println(Math.floor(12.1));
System.out.println("--------");
// public static int round(float a):按照四舍五入返回最接近参数的int型整数
System.out.println(Math.round(16.80F));
System.out.println(Math.round(16.01F));
System.out.println("--------");
//public static int max(int a, int b):返回两个int值中的较大值
System.out.println(Math.max(66, 88));
System.out.println("--------");
// public static int min(int a, int b):返回两个int值中的较小值
System.out.println(Math.min(55, 26));
System.out.println("--------");
// public static double pow(double a, double b):返回a的b次幂的值
System.out.println(Math.pow(3.0, 4.0));
System.out.println("--------");
//public static double random():返回值为double的正值,[0.0,1.0)
System.out.println(Math.random());
//生成1~100之间的随机数整数
System.out.println((int) (Math.random() * 100) + 1);
}
}
输出结果如下:
66 66 -------- 12.0 16.0 20.0 -------- 20.0 16.0 12.0 -------- 17 16 -------- 88 -------- 26 -------- 81.0 -------- 0.916361207103869 53



