常用类————(1)Math类

在我们进行基础JAVA编程时,我们会使用到很多类,数学运算是我们肯定避免不了的一种,所以Math类十分常用而且重要。


Math类

 第一步:打开API搜索Math。

第二步:查看成员变量:

代码示例:

public class Mathh {
	public static void main(String[] args) {
		System.out.println(Math.E);
		System.out.println(Math.PI);
	}
}	

输出结果:

2.718281828459045
3.141592653589793

Math中没有构造方法,直接来看方法;因为太多,直接在代码中演示常用的:

public class Mathh {
	public static void main(String[] args) {
		//abs方法,求绝对值方法,重载的有double型,float型,int型,long型
		System.out.println(Math.abs(-3443.344));
		
		//cbrt方法,开立方根
		System.out.println(Math.cbrt(27));
		
		//ceil向上取整,floor向下取整
		System.out.println(Math.ceil(3.444));
		System.out.println(Math.floor(3.444));
		
		//round方法,四舍五入返回一个整数
		System.out.println(Math.round(1.45));
		System.out.println(Math.round(1.51));
		
		//random方法,产生一个大于等于0,小于1的随机数
		System.out.println(Math.round(Math.random()*10+1));
	}
}	

输出结果:

3443.344
3.0
4.0
3.0
1
2
8

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值