• 字符串的练习


    1.不用startsWith和endsWith的方法,判断开头结束的字符

    String str = "adbckfnl";
            System.out.println("字符串为 "+str);
            Scanner sc =new Scanner(System.in);
            System.out.print("请输入开头字符");
            String a=sc.nextLine();
            String starts =str.substring(0, 2);
            if(a.equals(starts)==true)
            {
                System.out.println("您输入的是开头字符  "+ " "+starts);
            }
            else
            {
                System.out.println("您输入的不是开头字符");
            }
            System.out.print("请输入结束字符");
            String b=sc.nextLine();
            String ends =str.substring(7,8);
            if(b.equals(ends)==true)
            {
                System.out.println("您输入的是结束字符" +" "+ends);
            }
            else
            {
                System.out.println("您输入的不是结束字符");
            }

    2.随机生成四位验证码,随机生成5次

     String str1 ="0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ";
             char[] array = new char[4];
             Random random = new Random();
             System.out.print("验证码为:");
             for(int i =0; i<4; i++)
             {           
              array[i]=str1.charAt(random.nextInt(61));         
             }
                     System.out.println(array);
                    Scanner cc =new Scanner(System.in);
                    System.out.println("请输入验证码:");
                    String zh =new String(array); 
                     for(int j=0;j<5;j++)
                    {
                        if(cc.nextLine().equals(zh))
                        {
                            System.out.println("输入正确");
                            break;
                        }
                         else
                         { if(j<4){
                             System.out.println("输入错误,请重新输入");
                               System.out.print("验证码为:");
                               for(int i =0; i<4; i++)
                               {           
                                array[i]=str1.charAt(random.nextInt(61));         
                               }
                               System.out.println(array);
                               zh=new String(array);
                               System.out.println("请输入验证码:");}
                         else
                         {
                             System.out.println("对不起,您已经不能输入了");
                         }
                         }
                        
                    }

  • 相关阅读:
    hadoop节点的增删
    hadoop集群搭建
    主从配置
    CentOS7ssh互信
    Java根据视频的URL地址,获取视频时长
    Mybatis-plus使用@Select注解使用IN查询不出数据的问题
    洗牌算法
    1525
    SpringBoot+Quartz+MySQL实现分布式定时任务
    微信小程序授权登录解密失败问题
  • 原文地址:https://www.cnblogs.com/miracle-0807/p/5875286.html
Copyright © 2020-2023  润新知