/ *
Title:
integer calculation between -10.8 to 5.9, the absolute value of greater than 6 or less than 2.1 the number of?
analysis:
- Now that you've determined the range, for circulation
- Start position -10 -10.8 be converted into two approaches:
2.1 Math.ceil methods may be used, upward (in the positive direction) rounding
2.2 revolutions becomes strong int, automatically discarding decimals - Each number is an integer, so stepping expression should be num ++, so every time +1.
- How to get the absolute value: Math.abs method.
- Once you have found a number, need to counter ++ statistics.
Note: If you use Math.ceil method, can become -10.8 -10.0. Note ++ can also double the.
* /
public class Demo04MathPractise {
public static void main(String[] args) {
// int count = 0; // 符合要求的数量
double count = 0; // 符合要求的数量
double min = -10.8;
double max = 5.9;
// 这样处理,变量i就是区间之内所有的整数 2.2
/*for (int i = (int) min; i < max; i++) {
int abs = Math.abs(i); // 绝对值
if (abs > 6 || abs < 2.1) {
System.out.println(i);
count++;
}
}*/
// 这样处理,变量i就是区间之内所有的整数 2.1
for (double i = Math.ceil(min); i < max; i++) {
double abs = Math.abs(i); // 绝对值
if (abs > 6 || abs < 2.1) {
System.out.println(i);
count++;
}
}
System.out.println("总共有:" + count); // 9
}
}