2 Math类
包:java.lang.Math
方法:(详见API文档)
public static double abs(double a)
:返回绝对值。public static double ceil(double a)
:向上取整public static double floor(double a)
:向下取整public static long round(double a)
:四舍五入
练习
计算在 -10.8 到 5.9 之间,绝对值大于 6 或者小于 2.1 的整数
double min = -10.8;
double max = 5.9;
int count = 0;
for (int i = (int) Math.ceil(min); i < max; i++) {
int abs = Math.abs(i);
if (abs > 6 || abs < 2.1) {
System.out.println(i);
count++;
}
}
System.out.println(count);