Math(数学工具类),构造器私有化,方法设置为静态方法
常用的方法:

public static void main(String[] args) {
//abs(int a),绝对值-10返回10,(负负得正)
int a =-11;
System.out.println(Math.abs(a));
//ceil(double a),返回double值,大于或相等于参数,向上取整数
double x = 12.8;
double y = 0.3235;
System.out.println("Math.ceil(" + x + ")=" + Math.ceil(x));
System.out.println("Math.ceil(" + y + ")=" + Math.ceil(y));
System.out.println("Math.ceil(-0.65)=" + Math.ceil(-0.65));
//floor(double a),返回double值,小于或相等于参数,向下取整数
System.out.println("Math.floor(" + x + ")=" + Math.floor(x));
System.out.println("Math.floor(" + y + ")=" + Math.floor(y));
System.out.println("Math.floor(-0.65)=" + Math.floor(-0.65));
//round(float a)将数字四舍五入,返回int类型
System.out.println("Math.round(" + x + ")=" + Math.round(x));
System.out.println("Math.round(" + y + ")=" + Math.round(y));
System.out.println("Math.round(-0.65)=" + Math.round(-0.35));
//pow(double a),返回a的b次幕的值
System.out.println("Math.pow=" + Math.pow(5,2));
//random();返回int随机数
int m = (int) (Math.random()*10);
System.out.println("Math.random=" + m);
}
853

被折叠的 条评论
为什么被折叠?



