JAVA-常用包类II

1、 Random类

  public static void main(String[] args){
      //Random其实是伪随机,他不是真正的随机,他是一个拥有自己种子的随机算法
      //之所以之前我们每次使用Random对象获取值都是随机的,是因为我们并没有指定种子号()
      //当我们给一个long类型的参数作为构造方法的形参,就是制定了一个随机号,他会执行这个逻辑
      Random random1=new Random(100);//创建一个Random对象
      Random random2=new Random(100);//创建一个Random对象
      for(int i=0;i<20;i++){//随机生成20个随机整数,并显示
          int num1=random1.nextInt(10);//返回下一个伪随机数,整型的
          int num2=random2.nextInt(10);//返回下一个伪随机数,整型的
          System.out.println("第"+(i+1)+"个随机数 num1是"+num1+",num2是:"+num2);
      }
  }

2、 String类

2.1、 length()方法

  //判断长度,一个小型案例
  public static void main(String[] args){
      Scanner input=new Scanner(System.in);
      System.out.println("请输入登录名:");
      String name=input.next();
      System.out.println("请输入登录密码:");
      String password=input.next();
      if(password.length()<6){
          System.out.println("请确定密码长度是否大于等于6位");
      }else{
          System.out.println("即将登录,请稍等…………");
      }
  }

2.2、 equals()方法

  
  //匹配你输入的值是否相同
  //String类重写这个方法,我们可以直接调用

2.3、 忽略大小写比较

  //进行判断,如果密码是Password就可以通过
  //我们这里密码忽略大小写
  //两种方法可以实现
  //第一种方法:把需要比较的字符串均变成大写toUpperCase()或者小写toLowCase()
  String passwordT1="Password".toUpperCase();
  password=password.toUpperCase();
  if(passwordT1.equals(password)){
       System.out.println("登录成功");
  }else{
       System.out.println("登陆失败,密码不匹配");
  }
  ​
  //第二种方法:使用String的equalsIgnoreCase()忽略大小写方法比较
  String passwordT2="Password";
  if(passwordT2.equalsIgnoreCase(password)){
       System.out.println("登录成功");
  }else{
       System.out.println("登陆失败,密码不匹配");
  }

2.4、 String字符串拼接

  public static void main(String[] args){
      String s="Hello";
      String t="World";
      System.out.println(s+t);
      System.out.println(s.concat(t));
  }

2.5、 常用提取方式


sbuString(int index);

  
  public static void main(String[] args){
      String s="Hello";
      String t="World";
      //拼接字符串
      System.out.println(s+t);//HelloWorld
      System.out.println(s.concat(t);//HelloWorld
                         
      //寻找下标位置
      System.out.println(s.index("l"));//2
      System.out.println(s.lastIndex("l"));//3
      
      //截取字符串     这里提到的数字是0  开始的下标位置
      //只写一个int说明从下标为int的数开始的保留下来,其余删除
      System.out.println(s.subString(2));//llo(2会保存)
      //从"World"变成"r"说明保存第一个参数,从第二个参数开始不保存
      //左闭右开[2,3)包括2,不包括3
      //从2开始截,截取到3位置,不过包含2,不包含3
      System.out.println(t.subString(2,3));
  }
  
  //通过怕字符串截取来判断邮箱是否正确
  public static void main(String[] args){
      Scanner input=new Scanner(System.in);
      System.out.println("请输入一个邮箱:");
      String pass=input.next();
      //获取@符号,如果@符号不存在就会返回负数
      //如果存在第一位会返回0,这两个情况我们都不需要
      if(pass.indexOf("@")>0){
          //截取"@"之后的内容
          String str=pass.subString(pass.indexOf("@"));
          //判断是否包含".",吐过不包含则不正确
          //"."不能是最后一位(记得length()要-1,因为indexOf从0开始数length从1开始数
          if(str.indexOf(".")>0&&str.indexOf(".")!=str.length()-1){
              System.out.println("邮箱格式正确!");
              return;
          }
      }
      System.out.println("邮箱格式不正确!");
  }
 

2.6、 splot方法

 public static void main(String[] args){
      //split方法的而参数是正则表达式,通过你正则条件截取
      //每遇到一个匹配的字符,删除字符,将之前内容插入一个String数组中
      //后面内容继续查找,直到全部查找不到,将最有内容添加到String数组
      //最后返回String[]
      String[] strs="你爱我 我爱你 蜜雪冰城甜蜜蜜 你爱我 我爱你".split(" ");
      System.out.println("你爱我 我爱你 蜜雪冰城甜蜜蜜 你爱我 我爱你");
      for(String str:strs){
          System.out.println(str);
      }
  }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值