天天看點

Java注冊系統驗證的簡單實作

實作使用者注冊功能

這是題目要求:

Java注冊系統驗證的簡單實作

關于細節實作寫在代碼中咯:

package 注冊系統;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 風離
 * @Date: 2020/10/19/13:59
 * @Description:
 */
public class zhuce {
    public static void main(String[] args) {
      User u1=new User();
      u1.getUsername();
    u1.getPassword();
     u1.getEmail();
      u1.showtest();
    }
}
class User{
    private String username;
    private String password;
    private String email;

    public String getUsername(){
        boolean panduan=true;
        String denglu[]={" ","_","'","\"","?"};
        String username="";
        while(panduan) {
            System.out.println("請輸入賬号:");
            Scanner input = new Scanner(System.in);
            username = input.nextLine();
            for(int i=0;i<denglu.length;i++)
            {
                if (username.length() >= 25 || username.contains(denglu[i])) {
                    System.out.println("輸入不符合規範請重新輸入");
                }
                else{
                    panduan=false;
                }
            }
        }
        this.username = username;
        return username;
    }
    public String getPassword(){
        //數字數組
        String ShuZi[]={"0","1","2","3","4","5","6","7","8","9"};
        //int ShuZi[]={0,1,2,3,4,5,6,7,8,9};
        //字母數組
        String ZiMu[]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"
        ,"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        //特殊字元數組
        String ZiFu[]={"_","!","@","#","$","%","^","&","*"};
        boolean panduan=true;
        String password="";
        while(panduan)
        {    System.out.println("請輸入密碼:");
             Scanner input = new Scanner(System.in);
             password = input.nextLine();
            if (password.length() <= 6 || password.length() >=18 || password.contains(" ") || password.contains("?")
        ) {
            System.out.println("輸入不符合規範請重新輸入");
        }
        else{
            panduan=false;
        }}
        int x=0,y=0;
        boolean a1=false,b1=false,c1=false;
        //判斷是否包含數字
        String n[]=new String[10];
        for(int i=0;i<=9;i++) {
             n[i]=String.valueOf(i);
             if(password.contains(n[i]))
             {
                 a1=true;
             }
        }
        //判斷是否包含字母
            for(int j=0;j<ZiMu.length;j++) {
                boolean b = password.contains(ZiMu[j]);
                if (b ==true) {
                    b1 = true;
                }
            }
            //判斷是否包含字元
                 for(int k=0;k<ZiFu.length;k++)
                 {   boolean c=password.contains(ZiFu[k]);
                 //這裡必須使用雙等号!!!
                    if(c==true)
                    {
                        c1=true;

                    }
                 }
        //同時包含數字,字母,特殊字元
        if(a1&&b1&&c1)
        {
            System.out.println("密碼強度高");
        }
        else if(a1&&c1||a1&&b1||b1&&c1)
        {
            System.out.println("密碼強度中");
        }
       else
        {
            System.out.println("密碼強度低");
        }
        this.password=password;
        return password;
    }
    public String getEmail(){
        boolean panduan=true;
        String email="";
        while(panduan) {
            System.out.println("請輸入郵箱:");
            Scanner input = new Scanner(System.in);
            email = input.nextLine();
            if(!email.contains("@")||appearNumber(email,"@")>1||email.startsWith("@")||email.endsWith("@"))
            {
                System.out.println("email格式輸入錯誤請重新輸入");
            }
            else{
                panduan=false;
            }

        }
      this.email=email;
      return  email;
    }
    //判斷字元串中包含幾個“@”
    public static int appearNumber(String a,String b)
    {
        int count=0;
        Pattern p=Pattern.compile(b);
        Matcher m=p.matcher(a);
        while(m.find()){
            count++;
        }
        return count;
    }
    //顯示賬号密碼和郵箱
    public void showtest(){
        System.out.println("賬号:"+username);
        System.out.println("密碼:"+password);
        System.out.println("郵箱:"+email);
    }
}
           

運作截圖:

Java注冊系統驗證的簡單實作
Java注冊系統驗證的簡單實作
Java注冊系統驗證的簡單實作