Java--常用类--练习题

    1. package com.model.homework;
      
      import java.util.Arrays;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/7/5 22:38
       * 演示日期类的实际应用
       */
      public class HomeWorkDemo01 {
          public static void main(String[] args) {
              String str="abcdefg";
              String reverse = reverse(str,1, 3);
              System.out.println(reverse);
              String s = reverse01(str, 1, 3);
              System.out.println(s);
          }
          public static String reverse(String str,int start,int end){
              StringBuilder stringBuilder = new StringBuilder();
              String substring = str.substring(start, end + 1);
              StringBuilder reverse = stringBuilder.append(substring).reverse();
              return str.substring(0,start)+reverse+str.substring(end+1);
          }
          public static String reverse01(String str,int start,int end){
              if (!(str!=null&&start>=00&&end<str.length()&&start<end)){
                  throw new RuntimeException("参数不正确");
              }
              char[] chars=str.toCharArray();
              char temp=' ';
              for (int i = start,j=end; j >i ; i++,j--) {
                  temp=chars[i];
                  chars[i]=chars[j];
                  chars[j]=temp;
              }
              return new String(chars);
          }
      }
  1.  

     

    1. package com.model.homework;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/7/5 23:05
       */
      public class HomeWorkDemo02 {
      
          public static void main(String[] args) {
              boolean verify = verify("123", "123456", "171832195@qq.com");
              if (verify){
                  System.out.println("注册成功");
              }else {
                  throw new RuntimeException("注册信息异常");
              }
              Boolean verify01 = verify01("123", "123456", "171832195@qq.com");
              if (verify01){
                  System.out.println("注册成功");
              }else {
                  throw new RuntimeException("注册信息异常");
              }
          }
          //第一中方式
          public static boolean verify(String username,String password,String email){
              if (verifyEmail(email)&&verifyPassword(password)&&verifyUserName(username)){
                  return true;
              }
              return false;
          }
          public static Boolean verifyUserName(String username){
              if (username.length()>=2&&username.length()<=4){
                  return true;
              }else {
                  throw new RuntimeException("用户名错误");
              }
          }
          public static Boolean verifyPassword(String password){
              if (password.length()==6){
                  try {
                      Integer.parseInt(password);
                      return true;
                  } catch (NumberFormatException e) {
                      System.out.println(e.getMessage());
                  }
              }else {
                  throw new RuntimeException("密码错误");
              }
              return false;
          }
          public static Boolean verifyEmail(String email){
              int index = email.lastIndexOf('@');
              int index1 = email.indexOf('.');
              if(index!=-1&&index1!=-1&&index1>index){
                  return true;
              }
              return false;
          }
      
      
      //第二种方式
          public static Boolean verify01(String username,String password,String email){
      
              if (!(username.length()>=2&&username.length()<=4)){
                  throw new RuntimeException("用户名错误");
              }
      
              if(!(password.length()==6&&isDigital(password))){
                  throw new RuntimeException("密码错误");
              }
              if(!(email.indexOf('@')!=-1&&email.indexOf('.')!=-1&&email.indexOf('.')>email.indexOf('@'))){
                  throw new RuntimeException("邮箱错误");
              }
              return true;
          }
          public static boolean isDigital(String password){
              char[] chars=password.toCharArray();
              for (int i = 0; i < chars.length; i++) {
                  if (chars[i]<'0'||chars[i]>'9'){
                      return false;
                  }
              }
              return true;
          }
      }
    1. package com.model.homework;
      
      import java.util.Locale;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/7/5 23:35
       */
      public class HomeWorkDemo03 {
          public static void main(String[] args) {
              String name="Zhang Zi Han";
              String changeName = changeName(name);
              System.out.println(changeName);
      
              String s = changeName01(name);
              System.out.println(s);
          }
          public static String changeName(String name){
              if (name==null){
                  throw new RuntimeException("名字格式错误");
              }
              String[] split = name.split(" ");
              StringBuilder builder = new StringBuilder();
              builder.append(split[2]);
              builder.append(",");
              builder.append(split[0]);
              builder.append(".");
              builder.append(split[1].substring(0, 1));
              return builder.toString();
          }
      
          public static String changeName01(String name){
              if (name==null){
                  System.out.println("姓名不能为空");
                  return null;
              }
              String[] split = name.split(" ");
      
              String format = String.format("%s,%s.%c", split[2], split[0], split[1].toUpperCase().charAt(0));
              return format;
          }
      }
    1. package com.model.homework;
      
      /**
       * @Description:测试类
       * @Author: 张紫韩
       * @Crete 2021/7/5 23:48
       */
      public class HomeWorkDemo04 {
          public static void main(String[] args) {
      
              String str="dafasdfdsfSFASFASFAS16513116516";
              count(str);
          }
          public static void count(String str){
              char[] chars=str.toCharArray();
              int num1=0;
              int num2=0;
              int num3=0;
              for (int i = 0; i < chars.length; i++) {
                  if (chars[i]>='0'&&chars[i]<='9'){
                      num1++;
                  }
                  if (chars[i]>='a'&&chars[i]<='z'){
                      num2++;
                  }
                  if (chars[i]>='A'&&chars[i]<='Z'){
                      num3++;
                  }
      
              }
              System.out.println("数字有"+num1+"个");
              System.out.println("小写字母有"+num2+"个");
              System.out.println("大写字母有"+num3+"个");
          }
      }
  2.  

posted @ 2021-07-06 00:01  张紫韩  阅读(159)  评论(0编辑  收藏  举报