• 字符串类的3个小题


    1. 用自己的算法实现startsWith和endsWith功能。

    import java.util.Scanner;
    
    public class zuoye22_shixianstartWith {
        public static void main(String[] args){
            Scanner sc=new Scanner(System.in);
            String str=sc.nextLine();
            char start=str.charAt(0);
            char end=str.charAt(str.length()-1);
            if(start=='a'){
                System.out.println("这个字符串以a开头!");
            }
            else{
                System.out.println("这个字符串不以a开头!");
            }
            if(end=='a'){
                System.out.println("这个字符串以a结尾!");
            }
            else{
                System.out.println("这个字符串不以a结尾!");
            }
        }
    }

    2.采用字符的移位方式实现字符文本加密解密。

    import java.util.Random;
    import java.util.Scanner;
    
    public class zuoye17_jiamiqi {
        public static void main(String[] args)
             {
                 System.out.println("请输入密码:");
                 Scanner sc = new Scanner(System.in);
                 String password1=sc.nextLine();
                 int[] num=new int[password1.length()];
                 int[] password2=new int[password1.length()];
                 char[] password3=new char[password1.length()];
                 Random a=new Random();
                 for(int i=0;i<password1.length();i++){
                     int s=a.nextInt(20);
                     num[i]=s;
                 }
                 if(password1!=""&&password1.length()>=6)
                 {
                     System.out.print("加密后的密码是:");    
                     for(int i=0;i<password1.length();i++)
                     {
                         password2[i]=password1.charAt(i)+num[i];
                         System.out.print(password2[i]);
                     }
                    System.out.println();    
                 }else
                    {
                        System.out.println("请输入合法密码!至少6位!");  
                    }
            //解密
                 System.out.print("解密后的密码是:");
                    for(int i=0;i<password2.length;i++)
                     {
                        int index =    password2[i]-num[i];
                        password3[i]=(char)(index);
                        System.out.print(password3[i]);
                    }
                 }
         }    

    3.随机生成4位验证码,由用户输入并验证是否输入正确,如果输入错误就生成新的验证码让用户重新输入,最多输入5次

    import java.util.Random;
    import java.util.Scanner;
    
    public class zuoye16_yanzhengma {
        public static void main(String[] args){
            Scanner sc=new Scanner(System.in);
            Random s=new Random();
            String shuru=new String();
            String yanzhengma2=new String();
            String num="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            int j=0;
            do{
                StringBuilder yanzhengma=new StringBuilder();
                System.out.print("请输如验证码:");
                 shuru=sc.nextLine();
                 for(int i=0;i<4;i++){
                     int a=s.nextInt(61);
                     yanzhengma.append(num.substring(a, a+1));
                 }
                 yanzhengma2=yanzhengma.toString();
                 System.out.println("输入验证码错误,验证码是:"+yanzhengma);
                 j++;
                 if(j==5)break;
            }while(!yanzhengma2.equals(shuru));
            if(yanzhengma2.equals(shuru)){
                System.out.println("输入验证码正确!");
            }
        }
    }

  • 相关阅读:
    为你的 Github 博客加个 CMS -「内容管理」
    Alpha、Beta、RC、GA、LTS等软件各个版本号的含义
    WPF圆形进度条
    初试WPF代码迁移Core WPF
    dumpbin查看(Exe,Dll)是32位还是64位
    Windows Live Writer使用SyntaxHighlighter代码着色插件
    C#调用EnumDevice获取设备信息
    C#获取设备(Audio和Video)名称 简单整理
    C# Winform 换肤
    C# Winform模仿百度日历
  • 原文地址:https://www.cnblogs.com/jingzhenhua/p/5878969.html
Copyright © 2020-2023  润新知