該代碼將輸入密碼,并檢查密碼是否包含字母和數(shù)字。如果沒有密碼,則兩個注冊都將繼續(xù)。當(dāng)我輸入字母和數(shù)字時,寄存器是完整的;當(dāng)我輸入數(shù)字時,只有寄存器會繼續(xù);當(dāng)我輸入字母時,只有寄存器是完整的而不包含數(shù)字。import java.io.*;public class Sample {static BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));public static void main (String[] args)throws Exception { boolean valid = false; boolean alphaCheck = false; boolean numCheck = false; char[] alpha = {'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'}; char[] num = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; do { System.out.println("Password must contain Letters and Numbers Only. (abc../ABC.., 123..)"); System.out.println("Special Characters are not Allowed."); System.out.print("Register Password: "); String password = dataIn.readLine(); char[] passwordToArray = password.toUpperCase().toCharArray(); // Check if password contains numbers for(char x: passwordToArray){ for(char y: num){ if(x==y){ numCheck = true; break; } } } // Check if password contains Letters for(char x: passwordToArray){ for(char y: alpha){ if(x==y){ alphaCheck = true; break; } } } if(!numCheck){ System.out.println("No Numbers Found."); } if(!alphaCheck){ System.out.println("No Letters Found."); } // Check if password contains both Alphabets and Numbers // if false Continue Register if((numCheck)&&(alphaCheck)){ System.out.println("Register Complete"); valid = true; }else{ System.out.println("Register Failed. Please Try again"); } }while (!valid) ;}}
添加回答
舉報
0/150
提交
取消