Math类的使用
时间: 2023-11-27 17:49:23 浏览: 42
Math类是Java中的一个数学类,提供了各种数学运算的方法。以下是Math类的一些常用方法:
1. Math.abs(x):返回x的绝对值。
2. Math.sqrt(x):返回x的平方根。
3. Math.pow(x, y):返回x的y次幂。
4. Math.ceil(x):返回大于或等于x的最小整数。
5. Math.floor(x):返回小于或等于x的最大整数。
6. Math.round(x):返回最接近x的整数。
7. Math.max(x, y):返回x和y中的最大值。
8. Math.min(x, y):返回x和y中的最小值。
9. Math.random():返回一个随机数,范围在[0,1)之间。
例如:
```
double x = 10.5;
double y = -5.3;
System.out.println(Math.abs(y)); // 5.3
System.out.println(Math.sqrt(x)); // 3.24037034920393
System.out.println(Math.pow(x, 2)); // 110.25
System.out.println(Math.ceil(x)); // 11.0
System.out.println(Math.floor(y)); // -6.0
System.out.println(Math.round(x)); // 11
System.out.println(Math.max(x, y)); // 10.5
System.out.println(Math.min(x, y)); // -5.3
System.out.println(Math.random()); // 随机数
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)