数学工具类---Java

java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作。

public static double abs(double num):获取绝对值。有多种重载。
public static double ceil(double num):向上取整。
public static double floor(double num):向下取整。
public static long round(double num):四舍五入。

Math.PI代表近似的圆周率常量(double)

 1 public class Demo03Math {
 2 
 3     public static void main(String[] args) {
 4         // 获取绝对值
 5         System.out.println(Math.abs(3.14)); // 3.14
 6         System.out.println(Math.abs(0)); // 0
 7         System.out.println(Math.abs(-2.5)); // 2.5
 8         System.out.println("================");
 9 
10         // 向上取整
11         System.out.println(Math.ceil(3.9)); // 4.0
12         System.out.println(Math.ceil(3.1)); // 4.0
13         System.out.println(Math.ceil(3.0)); // 3.0
14         System.out.println("================");
15 
16         // 向下取整,抹零
17         System.out.println(Math.floor(30.1)); // 30.0
18         System.out.println(Math.floor(30.9)); // 30.0
19         System.out.println(Math.floor(31.0)); // 31.0
20         System.out.println("================");
21 
22         System.out.println(Math.round(20.4)); // 20
23         System.out.println(Math.round(10.5)); // 11
24     }
25 
26 }

 

 

 

练习题:

题目:
计算在-10.8到5.9之间,绝对值大于6或者小于2.1的整数有多少个?

分析:
1. 既然已经确定了范围,for循环
2. 起点位置-10.8应该转换成为-10,两种办法:
    2.1 可以使用Math.ceil方法,向上(向正方向)取整
    2.2 强转成为int,自动舍弃所有小数位
3. 每一个数字都是整数,所以步进表达式应该是num++,这样每次都是+1的。
4. 如何拿到绝对值:Math.abs方法。
5. 一旦发现了一个数字,需要让计数器++进行统计。

备注:如果使用Math.ceil方法,-10.8可以变成-10.0。注意double也是可以进行++的。
分析

 1 public class Demo04MathPractise {
 2 
 3     public static void main(String[] args) {
 4         int count = 0; // 符合要求的数量
 5 
 6         double min = -10.8;
 7         double max = 5.9;
 8         // 这样处理,变量i就是区间之内所有的整数
 9         for (int i = (int) min; i < max; i++) {
10             int abs = Math.abs(i); // 绝对值
11             if (abs > 6 || abs < 2.1) {
12                 System.out.println(i);
13                 count++;
14             }
15         }
16 
17         System.out.println("总共有:" + count); // 9
18     }
19 
20 }

 

posted @ 2020-07-22 20:40  云谷の风  阅读(181)  评论(0编辑  收藏  举报