A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lilongbin2016 中级黑马   /  2016-12-11 12:43  /  664 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

一、Random(随机)类  主要作用是获取随机数

方法1:nextInt(int x);    随机数取值范围:[0,x)
eg:  Random rd=new Random();
          int x=rd.nextInt(10)+1;//[0,9]+1为[1,10]之间的随机数
          System.out.println("产生的随机数是:"+x);

方法2:Math类里面的Math.random;     随机数取值范围:[0.0,1.0)
            (int)(Math.random()*10);    随机数取值范围:[0,9]
eg:int d=(int)(Math.random()*10);//[0,9]之间的随机数
        System.out.println("产生的随机数是:"+d);

一、Math(数学)类  用法    java.lang.*

Math.abs();    求绝对值的方法
eg:  int x1=Math.abs(-10);
          int x2=Math.abs(10);
          System.out.println(x1==x2);//true

Math.floor();     返回值是小于等于参数的最大整数
eg:  System.out.println(Math.floor(15.96));//15.0
       System.out.println(Math.floor(-15.96));//-16.0

Math.max(int x,int y);    返回x,y中最大值
Math.min(int x,int y);     返回x,y中最小值
eg:  System.out.println(Math.max(3,5));//5
          System.out.println(Math.min(3,5));//3

Math.pow(double x,double y);
eg:  System.out.println(Math.pow(2, 3));//8  返回2的3次方

Math.round(double x);    四舍五入
eg: System.out.println(Math.round(12.56));//13
         System.out.println(Math.round(12.46));//12

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马