Math类---相关操作

Math类—相关操作

  • abs—ceil—floor—round

    package cn.xiaoge.day08.demo04;
    
    /*
    java.util.Math类是数学相关的工具类, 里面提供了大量的静态方法, 完成与数学运算相关的操作.
    
    public static double abs(double num): 获取绝对值.
    public static double ceil(double num): 向上取证.
    public static double floor(double nm): 向下取证.
    public static long round(double num): 四舍五入.
    
    Math.PI代表近似的圆周率常量(double).
    */
    
    public class Demo03Math {
    
        public static void main(String[] args) {
            // 获取绝对值
            System.out.println(Math.abs(3.14));   // 3.14
            System.out.println(Math.abs(0));  // 0
            System.out.println(Math.abs(-2.5));  // 2.5
            System.out.println("======================");
    
            // 向上取证
            System.out.println(Math.ceil(19.3)); // 20.0
            System.out.println(Math.ceil(19.8)); // 20.0
            System.out.println(Math.ceil(19.0)); // 19.0
            System.out.println("======================");
    
            // 向下取证, 抹零
            System.out.println(Math.floor(18.9)); // 18.0
            System.out.println(Math.floor(18.0)); // 18.0
            System.out.println(Math.floor(18.4)); // 18.0
            System.out.println("======================");
    
            // 四舍五入
            System.out.println(Math.round(30.4));  // 30
            System.out.println(Math.round(30.5));   // 31
        }
    
    }
    
    // 运行结果
    3.14
    0
    2.5
    ======================
    20.0
    20.0
    19.0
    ======================
    18.0
    18.0
    18.0
    ======================
    30
    31
    
  • 练习题

    package cn.xiaoge.day08.demo04;
    
    /*
    题目:
    计算-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也是可以进行++的.
     */
    
    public class Demo04MathPractise {
        public static void main(String[] args) {
            int count1 = 0; // 符合要求的数量
            int count2 = 0; // 符合要求的数量
    
            double min = -10.8;
            double max = 5.9;
    
            // 这样处理, 变量i就是期间之内所有的整数
            for (int i = (int)min; i < max; i++) {
                if (Math.abs(i) > 6 || Math.abs(i) < 2.1) {
                    System.out.print(i + " ");
                    count1++;
                }
            }
            System.out.println("");
            System.out.println("总共有: " + count1);
            System.out.println("=========================");
    
            // 这样处理, 变量i就是期间之内所有的整数
            for (double i = Math.ceil(min); i < max; i++) {
                if (Math.abs(i) > 6 || Math.abs(i) < 2.1) {
                    System.out.print(i + " ");
                    count2++;
                }
            }
            System.out.println("");
            System.out.println("总共有: " + count2);
        }
    }
    
    // 运行结果
    -10 -9 -8 -7 -2 -1 0 1 2 
    总共有: 9
    =========================
    -10.0 -9.0 -8.0 -7.0 -2.0 -1.0 0.0 1.0 2.0 
    总共有: 9
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只因为你温柔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值