Chinaunix首页 | 论坛 | 博客
  • 博客访问: 243979
  • 博文数量: 164
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 1129
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 21:55
文章分类

全部博文(164)

文章存档

2017年(2)

2015年(67)

2014年(95)

我的朋友

分类: Java

2015-05-01 14:36:01



点击(此处)折叠或打开

  1. /*
  2.  * Math:用于数学运算的类。
  3.  * 成员变量:
  4.  *         public static final double PI
  5.  *         public static final double E
  6.  * 成员方法:
  7.  *         public static int abs(int a):绝对值
  8.  *        public static double ceil(double a):向上取整
  9.  *        public static double floor(double a):向下取整
  10.  *        public static int max(int a,int b):最大值 
  11.  *        public static double pow(double a,double b):a的b次幂
  12.  *        public static double random():随机数 [0.0,1.0)
  13.  *        public static int round(float a) 四舍五入
  14.  *        public static double sqrt(double a):正平方根
  15.  */
  16. public class MathDemo {
  17.     public static void main(String[] args) {
  18.         // public static final double PI
  19.         System.out.println("PI:" + Math.PI);
  20.         // public static final double E
  21.         System.out.println("E:" + Math.E);
  22.         System.out.println("--------------");

  23.         // public static int abs(int a):绝对值
  24.         System.out.println("abs:" + Math.abs(10));
  25.         System.out.println("abs:" + Math.abs(-10));
  26.         System.out.println("--------------");

  27.         // public static double ceil(double a):向上取整
  28.         System.out.println("ceil:" + Math.ceil(12.34));
  29.         System.out.println("ceil:" + Math.ceil(12.56));
  30.         System.out.println("--------------");

  31.         // public static double floor(double a):向下取整
  32.         System.out.println("floor:" + Math.floor(12.34));
  33.         System.out.println("floor:" + Math.floor(12.56));
  34.         System.out.println("--------------");

  35.         // public static int max(int a,int b):最大值
  36.         System.out.println("max:" + Math.max(12, 23));
  37.         // 需求:我要获取三个数据中的最大值
  38.         // 方法的嵌套调用
  39.         System.out.println("max:" + Math.max(Math.max(12, 23), 18));
  40.         // 需求:我要获取四个数据中的最大值
  41.         System.out.println("max:"
  42.                 + Math.max(Math.max(12, 78), Math.max(34, 56)));
  43.         System.out.println("--------------");

  44.         // public static double pow(double a,double b):a的b次幂
  45.         System.out.println("pow:" + Math.pow(2, 3));
  46.         System.out.println("--------------");

  47.         // public static double random():随机数 [0.0,1.0)
  48.         System.out.println("random:" + Math.random());
  49.         // 获取一个1-100之间的随机数
  50.         System.out.println("random:" + ((int) (Math.random() * 100) + 1));
  51.         System.out.println("--------------");

  52.         // public static int round(float a) 四舍五入(参数为double的自学)
  53.         System.out.println("round:" + Math.round(12.34f));
  54.         System.out.println("round:" + Math.round(12.56f));
  55.         System.out.println("--------------");
  56.         
  57.         //public static double sqrt(double a):正平方根
  58.         System.out.println("sqrt:"+Math.sqrt(4));
  59.     }
  60. }

阅读(487) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~