xoxobool

成功者,永远成功,失败者,永远失败,我要面对者,走向成功!

导航

字符串的练习

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("对不起,您已经不能输入了");
                     }
                     }
                    
                }

 

posted on 2016-09-15 20:32  春之林木  阅读(185)  评论(0编辑  收藏  举报