/**
Math类中常用方法
*
/
public class M {
public static void main(String[] args) {
/
-
Math的静态常量:
Math.PI:表示圆周率
Math.E:自然对数
/
System.out.println(Math.PI);
//输出3.141592653589793
System.out.println(Math.E);
//输出2.718281828459045(自然对数)
/* -
Math.abs求绝对值 数值为double、int、float、long类型都可以
/
System.out.println(Math.abs(-110));//输出110
System.out.println(Math.abs(110.110));//输出110.11
/* -
Math.ceil返回x轴靠右的整数值。 例:11.1返回12.0 数值为double类型
/
System.out.println(Math.ceil(11.1));//输出12.0
System.out.println(Math.ceil(-11.1));//输出-11.0
/* -
Math.floor返回x轴靠左的整数值。 例:11.1返回11.0 数值为double类型
*/System.out.println(Math.floor(11.1));//输出11.0 System.out.println(Math.floor(-11.1));//输出-12.0
/**
-
Math.max Math.min分别表示在两者之间选最大值和最小值
-
例:max(110,119)输出119 min(120,110)输出110
-
数值为double、int、float、long类型都可
/
System.out.println(Math.max(110,119 ));//输出119
System.out.println(Math.min(110,119 ));//输出110
/* -
Math.pow 求幂运算 例:Math.pow(2,3) 输出8(2为底数,3为指数)
-
其中Math.sqrt为开平方,Math.cbrt为开立方
-
只能以double类型
/
System.out.println(Math.pow(2,3));//输出8.0
System.out.println(Math.sqrt(8));//输出2.8284271247461903
System.out.println(Math.cbrt(8));//输出2.0
/* -
Math.round四舍五入运算 例: 4.5输出5
-
类型为double、float类型
/
System.out.println(Math.round(4.5));//输出5
/* -
Math.random显示0到1的随机数
*/
Random r=new Random();
int a=r.nextInt(100)+1;//产生1-100的随机数
System.out.println(a);}
}