Math类和Random类

Math类

abs 绝对值
sin,cos,tan,atan,acos,asin 三角函数
sqrt 平方根
pow(double a,double b) a的b次幂
max(double a,double b) 取最大值
min(double a,double b) 取最小值
ceil(double a) 大于a的最小整数
floor(double a) 小于a的最大整数
random() 返回0.0到1.0的随机数
long round(double a) double类型的a转为long类型的a(四舍五入)
toDegrees(double angrad) 弧度转角度
toRadians(double angdeg)角度转弧度

Random类

帮助生成随机数

package 常用类;

import java.util.Random;

public class TestRandom {
public static void main(String[] args) {
    Random rand=new Random();
    //随机生成【0,1)之间的double类型的数据
    System.out.println(rand.nextDouble());
    //随机生成int类型允许范围内的整型数据
    System.out.println(rand.nextInt());
    //随机生成【0,1)之间的float类型的数据
    System.out.println(rand.nextFloat());
    //随机生成true或false
    System.out.println(rand.nextBoolean());
    //随机生成【0,10)之间的int类型的数据
    System.out.println(rand.nextInt(10));
    //随机生成【20,30)的int类型的数据
    System.out.println(20+rand.nextInt(10));
    //随机生成【20,30)的int类型的数据//同上,计算方法不同
    System.out.println(20+(int)(rand.nextDouble()*10));
    
}
}

结果:
0.5842756723428777
-372540183
0.16897333
false
6
26
27

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容