> 文档中心 > Java学习笔记<四>(常用类:Math;随机数Random)

Java学习笔记<四>(常用类:Math;随机数Random)


Math

 /*Math*/ //Math类是静态方法,不必创建对象,直接使用 //取整相关操作 System.out.println(Math.ceil(3.4));     //向上取整  4.0 System.out.println(Math.floor(3.4));    //向下取证  3.0 System.out.println(Math.round(3.4));    //3  四舍五入  Math.floor(x+0.5) System.out.println(Math.round(3.5));    //4 //绝对值 开方 a的N次幂等操作 System.out.println(Math.abs(-5));//绝对值  5 System.out.println(Math.sqrt(64));      //开方值  8.0 System.out.println(Math.pow(3, 4));     //3的4的次值  81.0 //Math类中常用的变量 System.out.println(Math.PI);//约等于3.14   3.141592653589793 System.out.println(Math.E); //科学计数法e  2.718281828459045 System.out.println(Math.random());    //[0,1)之间随机生成浮点数   例如: 0.6528720302999838

Random

 /*Random*/ //创建随机数生成对象,使用对象 调用 Random类的方法 Random random = new Random(); System.out.println(random.nextDouble()); //0.6520020361149323  随机生成double值 System.out.println(random.nextInt());    //408755493    随机生成int值 System.out.println(random.nextFloat());  //0.9225017    随机生成float值 System.out.println(random.nextBoolean());//false 随机生成boolean值 System.out.println(random.nextInt(10));//随机生成[0,10)中的int值 System.out.println(random.nextInt(10)+20);    //随机生成[20,30)中的int值 System.out.println(20+(int)(random.nextDouble()*8));  //返回随机生成的double值*8+20的结果(int类型) 例如:26

海量搞笑GIF动态图片