字符串

标签:算法   class   style   log   com   http   it   la   sp   

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("输入验证码正确!");
        }
    }
}

技术分享

posted @ 2016-09-18 06:37  hanruyue  阅读(184)  评论(0编辑  收藏  举报