package liu0914; import java.util.Random; import java.util.Scanner; public class Yanzhengma { public static void main(String[] args) { String str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; char[]arr=new char[4];//定義一個長度是4的char型數組 Random sj=new Random(); System.out.println("驗證碼是:"); for(int i=0;i<4;i++) { arr[i]=str.charAt(sj.nextInt(61));//從str中隨機截取4個單個字符並賦值給arr這個數組存放 } System.out.println(arr); Scanner sc=new Scanner(System.in); System.out.println("請輸入驗證碼"); String a=new String(arr);//把數組轉換成字符串 //定義輸入次數 for(int j=0;j<5;j++) { if(sc.nextLine().equals(a)) { System.out.println("驗證碼輸入正確"); } else { System.out.println("驗證碼輸入有誤,請重新輸入"); if(j<=3) { System.out.print("請輸入驗證碼"); for(int i=0;i<4;i++) { arr[i]=str.charAt(sj.nextInt(61));//從str中隨機截取4個單個字符並賦值給arr這個數組存放 } System.out.println(arr); a=new String (arr); } else { System.out.println("輸入有誤,對不起,5次機會已用完"); } } } } }