Math类的常用方法和静态导入.

Love The Way You Lie 2021-10-20 00:54 390阅读 0赞
  1. 1 package Test;
  2. 2 /**
  3. 3 * 测试Math的常用方法和静态导入
  4. 4 * @author 小王同学
  5. 5 *
  6. 6 */
  7. 7 import static java.lang.Math.*;//使用静态导入以后可以直接使用Math中的常量,不用再使用Math.常量.
  8. 8 public class TestMath {
  9. 9 public static void main(String[] args) {
  10. 10 //测试Math类的常用方法
  11. 11 //取整相关操作
  12. 12 System.out.println(Math.ceil(3.2));//向上取整后返回double
  13. 13 System.out.println(ceil(3.2));//使用静态导入以后
  14. 14 System.out.println(Math.floor(3.2));//向下取整返回double
  15. 15 System.out.println(floor(3.2));//向下取整返回double
  16. 16 System.out.println(Math.round(3.2));//四舍五入
  17. 17 System.out.println(round(3.2));//四舍五入
  18. 18 System.out.println(Math.round(3.8));//四舍五入
  19. 19 //绝对值、开方、a的b次幂等操作
  20. 20 System.out.println(Math.abs(-45));//求绝对值
  21. 21 System.out.println(Math.sqrt(64));//64开平方
  22. 22 System.out.println(Math.pow(5, 2));//5的平方
  23. 23 System.out.println(Math.pow(2, 5));//2的5次方
  24. 24 //Math类中常用的常量
  25. 25 System.out.println(Math.PI);
  26. 26 System.out.println(Math.E);
  27. 27 //随机数
  28. 28 System.out.println(Math.random());// 得到[0,1)的随机数.
  29. 29 }
  30. 30
  31. 31 }

1726984-20190713201042998-774684595.png

今天是参加北京尚学堂卓越班的第6天加油!

转载于:https://www.cnblogs.com/xw1024/p/11181899.html

发表评论

表情:
评论列表 (有 0 条评论,390人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java Math方法

    Java Math类的常用方法 在 Java 中 Math 类封装了常用的数学运算,提供了基本的数学操作,如指数、对数、平方根和三角函数等。 绝对值 方法: