黑马程序员技术交流社区
标题:
Math类
[打印本页]
作者:
linder_qzy
时间:
2015-3-16 14:47
标题:
Math类
Math
也是一个工具类,里边都是静态方法,包含用于执行基本数学运算的方法.
主要方法:
Math.ceil(); 返回大于指定数据的最小整数
Math.floor();返回小于指定数据的最大整数
Math.round();四舍五入
Math.abs();返回绝对值
Math.max(a,b);返回最大值
Math.min(a, b);返回最小值
Math.pow(a,b);返回a的b次幂
Math.random();返回随机的double值,该值大于等于 0.0 且小于 1.0
public class Demo05_Math {
public static void main(String[] args) {
// 返回十次1到10的随机数
for (int x = 0; x < 5; x++) {
int d = (int) (Math.random() * 10 + 1);
System.out.println(d);
}
double d = Math.ceil(16.34);
double d1 = Math.floor(-12.54);
double d2 = Math.pow(2, 3);
long l = Math.round(12.54);
System.out.println("d-->" + d);
System.out.println("d1-->" + d1);
System.out.println("d2-->" + d2);
System.out.println("l-->" + l);
}
}
/*
* 输出结果:
* 4
* 4
* 5
* 2
* 8
* d-->17.0
* d1-->-13.0
* d2-->8.0
* l-->13
*/
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2