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);
        }
相关推荐
Java牛马3 小时前
SpringBoot Starter 依赖相关总结
java·spring·springboot·starter·自动装配·依赖
丰锋ff3 小时前
基于 Qt 的智能门禁系统(二)
开发语言·qt
吴声子夜歌3 小时前
Java面试——Spring Cloud原理及应用(二)
java·spring cloud·面试
fpcc3 小时前
跟我学C++中级篇——编译期的条件选择
开发语言·c++
SomeB1oody4 小时前
【RustyML入门】6.3. 并行归约
开发语言·后端·机器学习·rust·教程
东小西4 小时前
【SAA实战】第 1 篇:ReactAgent 入门——先撸个"会调工具的助手"跑起来
java·人工智能·spring
mqiqe5 小时前
AgentScope Java 2.0 协议集成全景解析:A2A、AG-UI、Agent Protocol 三大开放协议实战指南
java·开发语言·ui
lzhdim5 小时前
12、JavaScript常见的内存泄露问题 - JavaScript学习系列文章
开发语言·前端·javascript·学习·ecmascript
脉动数据行情5 小时前
Java 实现台股 TWSE/TPEx 行情采集(个股 + K 线)
java·开发语言·twse·tpex·台股
爱学习的小邓同学5 小时前
Golang --- (1)第一个Golang程序
开发语言·后端·golang