Random类和String类

Random类:

java.util.Random类

生成随机数:

上面的Math类的random()方法也可以产生随机数,其实Math类的random()方法底层就是用Random类实现的。

复制代码
Random rand = new Random();  //创建一个Random对象
获取随机数的方法:

|---------------------|---------------|
| int nextInt(); | 返回下一个随机数 |
| int nextInt(int n); | 返回0到n-1之间的随机数 |

例子:生成0-20随机数:
复制代码
public static void main(String[] args) {
        Random rand = new Random();
        //生成20个随机数并且显示
        for (int i = 0; i<20;i++){
            int num = rand.nextInt(10);
            System.out.println("第"+i+"个随机数是:"+num);
        }
    }
种子值:

用同一个种子值来初始化两个Random 对象,然后用每个对象调用相同的方法,得到的随机数也是相同的。

种子的数值不同代表不同的状态,没什么实际意义

复制代码
 public static void main(String[] args) {
        // 使用相同的种子值初始化两个 Random 对象
        long seed = 42;
        long seed2 = 23;
        Random random1 = new Random(seed);
        Random random2 = new Random(seed);
        // 打印两个 Random 对象的随机数
        System.out.println("Random1: " + random1.nextInt(100));
        System.out.println("Random2: " + random2.nextInt(100));
        // 再次打印,看看结果是否一致
        System.out.println("Random1: " + random1.nextInt(100));
        System.out.println("Random2: " + random2.nextInt(100));

         Random random3 = new Random(seed2);
         System.out.println(random3.nextInt(200));
         System.out.println(random3.nextInt(200));
     
    }

String类:

String类位于java.lang包中,具有丰富的方法计算字符串的长度、比较字符串、连接字符串、提取字符串

1.length()方法:

返回字符串中的字符数

2.equals( )方法

比较存储在两个字符串对象的内容是否一致

比较原理:检查组成字符串内容的字符是否完全一致

"=="和equals()区别:

==:判断两个字符串在内存中的地址,即判断是否是同一个字符串对象

字符串比较的其他方法:
使用equalsIgnoreCase()忽略大小写:
复制代码
public static void main(String[] args) {
        String str1 = "AsapBayby";
        String str2 = "asApBayBy";
        if (str1.equalsIgnoreCase(str2)){
            System.out.println("忽略大小写相等");
        }
    }
使用toLowerCase()小写:

比较方法:str1.toLowerCase().equals(str2.toLowerCase())

复制代码
 public static void main(String[] args) {
        String str1 = "AsapBayby";
        String str2 = "asApBayBy";
        if (str1.toLowerCase().equals(str2.toLowerCase())){
            System.out.println("都转成小写相同");
            System.out.println(str2);
        }else {
            System.out.println("忽略大小写不相等");
        }

    }
使用toUpperCase()大写:

同理,比较条件换成:

复制代码
 if (str1.toUpperCase().equals(str2.toUpperCase())){
            System.out.println("都转成大写相同");
            System.out.println(str2);
        }
相关推荐
CodeSheep程序羊12 分钟前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展
独好紫罗兰12 分钟前
对python的再认识-基于数据结构进行-a002-列表-列表推导式
开发语言·数据结构·python
I'mChloe20 分钟前
PTO-ISA 深度解析:PyPTO 范式生成的底层指令集与 NPU 算子执行的硬件映射
c语言·开发语言
编程小白202632 分钟前
从 C++ 基础到效率翻倍:Qt 开发环境搭建与Windows 神级快捷键指南
开发语言·c++·windows·qt·学习
我是咸鱼不闲呀32 分钟前
力扣Hot100系列19(Java)——[动态规划]总结(上)(爬楼梯,杨辉三角,打家劫舍,完全平方数,零钱兑换)
java·leetcode·动态规划
像风一样的男人@1 小时前
python --读取psd文件
开发语言·python·深度学习
输出输入1 小时前
前端核心技术
开发语言·前端
加油,小猿猿1 小时前
Java开发日志-双数据库事务问题
java·开发语言·数据库
薛定谔的猫喵喵1 小时前
天然气压力能利用系统综合性评价平台:基于Python和PyQt5的AHP与模糊综合评价集成应用
开发语言·python·qt
yuluo_YX1 小时前
Reactive 编程 - Java Reactor
java·python·apache