常用类—Random,Math,Interger

Random类和Math类其中Random主要作用是获取随机数

一、Random(主要获取随机数字)

1.Random—nextInt方法

//Random获取30-50的随机数
Random random = new Random();
while(true) {
    int nextInt = random.nextInt(20)+30;
    System.out.println(nextInt);
}
//输出30-50的随机数,注意是[30-50),左闭右开

2.Random其他常用方法

Random random = new Random();
System.out.println("生成boolean类型的随机数:"+random.nextBoolean());
System.out.println("生成[0,1.0)区间的double类型的随机数:" +random.nextDouble());
System.out.println("生成float类型的随机数:" +random.nextFloat());
System.out.println("生成0到10之间int类型的随机数:" +random.nextInt(10));
System.out.println("生成long类型的随机数:" +random.nextLong());
System.out.println("生成[0,5.0)区间的小数:" +random.nextDouble()*5);
//输出结果依次如下
//生成boolean类型的随机数:true
//生成[0,1.0)区间的double类型的随机数:0.2782692116346033
//生成float类型的随机数:0.4092505
//生成0到10之间int类型的随机数:1
//生成long类型的随机数:3122214338861374454
//生成[0,5.0)区间的小数:0.2446341535292107

二、Math

1.Math—random方法

//Math获取30-50的随机数
int random = (int) (Math.random()*20+30);
while(true) {
    System.out.println(random);
}
//也是输出30-50的随即数,注意是[30-50),左闭右开

2.Math其他常用的方法

System.out.println("输出其中的最小值:"+Math.min(6, 7));
System.out.println("输出括号数字的绝对值:"+Math.abs(-5));
System.out.println("输出这两个数字相加的结果:"+Math.addExact(6,-6));
System.out.println("输出括号向上取整的数字:"+Math.ceil(4.44));
System.out.println("输出括号向下取整的数字:"+Math.floor(4.5));
System.out.println("输出括号四舍五入的数字:"+Math.round(4.45));
System.out.println("输出括号中的算术平方根:"+Math.sqrt(16));
System.out.println("输出括号中的立方根:"+Math.cbrt(8));
System.out.println("输出括号中2的3次方,其他数字以此改写:"+Math.pow(2,3));
//输出结果依次如下
//输出其中的最小值:6
//输出括号数字的绝对值:5
//输出这两个数字相加的结果:0
//输出括号向上取整的数字:5.0
//输出括号向下取整的数字:4.0
//输出括号四舍五入的数字:4
//输出括号中的算术平方根:4.0
//输出括号中的立方根:2.0
//输出括号中2的3次方,其他数字以此改写:8.0

三、Integer

1.Integer—字符串数字相互转换

//字符串转数字
String num="10";
int num1=Integer.parseInt(num);
System.out.println(num1+1);
//数字转字符串 注意:如果Integer中的num存储的是中文,则报出异常:NumberFormatException
String num2=10+"989899";
System.out.println(num2);
//输出
//11
//10989899-这是拼接了

2.Integer—进制

//进制
int num = 40;
String str = Integer.toString(num); // 将数字转换成字符串
String str1 = Integer.toBinaryString(num); // 将数字转换成二进制
String str2 = Integer.toHexString(num); // 将数字转换成八进制
String str3 = Integer.toOctalString(num); // 将数字转换成十六进制
System.out.println(str + "的二进制数是:" + str1);
System.out.println(str + "的八进制数是:" + str3);
System.out.println(str + "的十进制数是:" + str);
System.out.println(str + "的十六进制数是:" + str2);
//输出
//40的二进制数是:101000
//40的八进制数是:50
//40的十进制数是:40
//40的十六进制数是:28

3.Integer—比较

Integer i = new Integer(100);
Integer j = new Integer(100);
System.out.println(i==j);            //false
System.out.println(i.equals(j));    //true
System.out.println("---------------");

Integer a = 500;        //此时a进行了装箱操作,注意Integer的只能存储127以下的,如果超过127就不在常量池里去拿
Integer b = 500;        //此时b也进行了装箱操作,两个数值上是相等的,但是并不是同一个对象。
System.out.println(a == b);            //false
System.out.println(a.equals(b));    //true
System.out.println("---------------");

//数据在byte范围内,JVM不会重新new对象。(可以查看源码)
Integer x = 127;
Integer y = 127;
System.out.println(x==y);            //true
System.out.println(x.equals(y));    //true
System.out.println("---------------");
//输出
false
true
---------------
false
true
---------------
true
true
---------------

posted @ 2022-10-13 11:09  花海~  阅读(103)  评论(0)    收藏  举报