package test;
import java.util.Random;
/*
Math 数学类, 主要是提供了很多的数学公式。
abs(double a) 获取绝对值
ceil(double a) 向上取整 取大的数
floor(double a) 向下取整 取小的数
round(float a) 四舍五入
random() 产生一个随机数. 大于等于 0.0 且小于 1.0 的伪随机 double 值
*/
public class Demo8 {
public static void main(String[] args) {
System.out.println("绝对值:"+Math.abs(-3));
System.out.println("向上取整:"+Math.ceil(3.14));
System.out.println("向下取整:"+Math.floor(-3.14)); //
System.out.println("四舍五入:"+Math.round(3.54));
System.out.println("随机数:"+Math.random());
//产生随机数
char[] arr = {'中','国','传','a','Q','f','B'};
StringBuilder sb = new StringBuilder();
Random random = new Random();
//需要四个随机数,通过随机数获取字符数组中的字符,
for(int i = 0 ; i< 4 ; i++){
int index = random.nextInt(arr.length); //产生的 随机数必须是数组的索引值范围之内的。
sb.append(arr[index]);
}
System.out.println("验证码:"+ sb);
}
}
Math与Random类-Java基础064
最新推荐文章于 2023-06-20 15:16:53 发布