Random类、String类

Random类

Random是位于java.util包中,用于生成随机数

Random rand = new Random();
// 随机生成一个int范围内的数
int num = rand.nextInt();
// 指定生成0到10的整数
int num2 = rand.nextInt(10);
// 指定种子
Random rand2 = new Random(2);
Random rand3 = new Random(2);
int num3 = rand.nextInt(10);
int num4 = rand.nextInt(10);// 使用相同种子产生的随机数相同,故num3和num4的值相同

String类

String类位于java.lang包中,是java中最常用的类之一

// 定义String类型的变量
String s = "abc";
String s1 = new String("abc");
// String类型比较:equals()
System.out.println(s==s1);// String是引用类型,用==比较是比较地址,由于s1是new出的对象,故结果为false
System.out.println(s.equals(s1));// String类型的equals()方法被重写,比较的是内容,故输出为true
// length()方法
System.out.println(s.length());// 输出3,需要注意的是,数组的length是属性,String的是方法
// 忽略大小比较:equalsIgnoreCase()
System.out.println(s.equalsIgnoreCase("Abc"));// true
// 去除首尾空格(若中间有空格不去除):trim()
String s2 = " abc ";
System.out.println(s.trim());// abc
// 字符串截取:substring(beginIndex,[endIndex]),下标从0开始,[begin,end)
System.out.println(s.substring(1));// bc
System.out.println(s.substring(1,2));// b
System.out.println(s.substring(1,3));// bc
// 判断是否已某字符开头startsWith()
System.out.println(s.startsWith("a")); // true
// 判断是否已某字符结束endsWith()
System.out.println(s.endsWith("c")); // true
// 分割字符串split(),按指定字符分割,返回String类型数组
String s3 = "a,b,c";
String[] str = s3.split(",");
// 字符串拼接concat(),等同于+号拼接
System.out.println(s.concat("d")+"e"); 
// 找出并返回第一个指定字符的下标,若没有则返回-1,indexOf()
System.out.println(s.indexOf("c")); 
// 转换成字符数组:toCharArray()
char[] c = s.toCharArray();

示例
实现会员注册,要求:
①用户名长度不小于3
②密码长度不小于6
③注册时两次输入密码必须相同

public class TestRegister {
    public static void main(String[] args) {
        int flag = 1;
        do{
            System.out.println("****欢迎进入注册系统****");
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名:");
            String name = sc.next();
            System.out.println("请输入密码:");
            String pass = sc.next();
            System.out.println("请再次输入密码:");
            String pass2 = sc.next();
            if(name.length()<3||pass.length()<6){
                System.out.println("用户名长度不能小于3,密码长度不能小于6");
                continue;
            }
            if(!pass.equals(pass2)){
                System.out.println("两次密码不同!");
                continue;
            }
            flag = 0;
        }while (flag == 1);
        System.out.println("注册成功!");
    }
}

输入一个字符串,再输入要查找的字符,判断该字符在该字符串中出现的次数

public class TestCount {
    public static void main(String[] args) {
        int count = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String str = sc.next();
        System.out.println("请输入要查找的字符:");
        String s = sc.next();
        char[] c = str.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if(s.toCharArray()[0]==(c[i])){
                count++;
            }
        }
        System.out.println("\""+str+"\"包含"+count+"个“"+s+"”");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值