java程序驗證用戶名密碼和驗證碼登錄的小例子


package Study02;
import java.util.Random;
import java.util.Scanner;
public class test {
	static String NAME="jie";
	static String PASS="123";
	public static void main(String[] args) {
		//輸入用戶名和密碼
		//每次登陸生成驗證碼
		//System.out.println(1==2);
		Scanner sc =new Scanner(System.in); 
		test ts=new test();
		while(true){
			System.out.println("請輸入用戶名:");
			String name=sc.next();
			System.out.println("請輸入密碼:");
			String pass=sc.next();
			//臨時變量temp記錄隨機碼
			String temp= ts.suijishu();
			System.out.println("請輸入驗證碼"+temp);
			String yzm=sc.next();
			//驗證碼正確
			if(yzm.equalsIgnoreCase(temp)){
				if(name.equals(NAME)&&pass.equals(PASS)){
					System.out.println("登陸成功!");
					break;
				}
				else{
					System.out.println("用戶名或密碼錯誤!");
				}
			}
			else{
				System.out.println("驗證碼錯誤!");
			}
		}
	}
	//生成隨機數驗證碼
	public static String suijishu(){
		Random rds=new Random();
		char neirong[]={'1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','A','B','C','D','E'};
		String code="";
		for(int i=0;i<6;i++){
			int tmp=rds.nextInt(neirong.length);
			code=code+neirong[tmp];
		}
		return code;
	}
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM