java.lang.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作。(提供了一系列静态方法用于科学计算。其方法的参数和返回类型一般为double型。)(Math类没有提供公开的构造器。)(私有化构造方法,所有的方法都是静态的。)
Math.abs ( 传入的值 ) 取绝对值 absolute 完全的,绝对的 Math.ceil ( 传入的值 ) 向上取整 Math.floor ( 传入的值 ) 向下取整 Math.round (传入的值 ) 四舍五入 Math.PI 代表近似的圆周率 常量(double) Math.random ( ) a pseudorandom double greater than or equal to 0.0 and less than 1.0.
大于或等于0.0且小于1.0的伪随机双精度。[0.0,1.0) 左闭右开
如何获取一个随机数:10 - 99 ?
Math.random() * 90 + 10
公式:[ a,b ] : (int) ( Math.random() * (b - a + 1 ) + a )
Math.max () 返回两个 int 值中的较大值 Math.min () 返回两个 int 值中的较小值 Math.pow (double a,double b) 返回 a 的 b 次幂的值 power 次幂, 能力,电 Math.sqrt (传入的值 a) 返回 a 的平方根 square root 平方根 ![]()
Math.cbrt (传入的值 a)
返回 a 的立方根 cube root 立方根
计算在 -10.8 到 5.9 之间,绝对值大于6 或者 小于2.1 的整数有多少个?
public class Demo04MathPractise { public static void main(String[] args) { int count = 0; double min = -10.8; double max = 5.9; for (int i = (int) min; i < max; i++) { int abs = Math.abs(i); if (abs > 6 || abs < 2.1) { System.out.print(i + " "); count++; } } System.out.println("总共有:" + count + "个"); } } D:\Java\jdk-17\bin\java.exe -10 -9 -8 -7 -2 -1 0 1 2 总共有:9个 Process finished with exit code 0
public class Demo03Math { public static void main(String[] args) { System.out.println(Math.abs (-3.14));//3.14 System.out.println(Math.ceil(3.4));//4.0 System.out.println(Math.floor(3.4));//3.0 System.out.println(Math.round(3.5));//4 数学中的四舍五入 System.out.println(Math.PI); // 3.141592653589793 System.out.println(Math.random()); System.out.println(Math.max(89, 12)); System.out.println(Math.min(89, 21)); System.out.println(Math.pow(2.0, 3.0)); System.out.println(Math.sqrt(4)); System.out.println(Math.cbrt(8)); } } D:\Java\jdk-17\bin\java.exe 3.14 4.0 3.0 4 3.141592653589793 0.7034166271154065 89 21 8.0 2.0 2.0 Process finished with exit code 0