Java learning -Math practice

/ *
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:

  1. Now that you've determined the range, for circulation
  2. 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
  3. Each number is an integer, so stepping expression should be num ++, so every time +1.
  4. How to get the absolute value: Math.abs method.
  5. 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
    }
}
Published 23 original articles · won praise 0 · Views 144

Guess you like

Origin blog.csdn.net/qq_44813352/article/details/104326916