// Java Math 类的示例代码
public class MathExample {
public static void main(String[] args) {
// 使用 Math.abs() 方法获取绝对值
int absValue = Math.abs(-10);
System.out.println("绝对值: " + absValue);
// 使用 Math.pow() 方法计算幂
double powerValue = Math.pow(2, 3);
System.out.println("2 的 3 次方: " + powerValue);
// 使用 Math.sqrt() 方法计算平方根
double sqrtValue = Math.sqrt(16);
System.out.println("16 的平方根: " + sqrtValue);
// 使用 Math.random() 方法生成随机数
double randomValue = Math.random();
System.out.println("随机数: " + randomValue);
// 使用 Math.max() 和 Math.min() 方法获取最大值和最小值
int maxValue = Math.max(5, 10);
int minValue = Math.min(5, 10);
System.out.println("最大值: " + maxValue);
System.out.println("最小值: " + minValue);
// 使用 Math.round() 方法进行四舍五入
float roundValue = Math.round(4.7f);
System.out.println("四舍五入: " + roundValue);
}
}
Math.abs():返回指定数值的绝对值。Math.pow():返回第一个参数的第二个参数次幂。Math.sqrt():返回指定数值的平方根。Math.random():返回一个介于 0.0(包括)和 1.0(不包括)之间的伪随机数。Math.max() 和 Math.min():分别返回两个数值中的较大值和较小值。Math.round():返回最接近的整数值,遵循四舍五入规则。下一篇:java编译命令
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站