這是我的工作,現在的任務...密碼驗證
實施一項計劃,它讀取用戶密碼並驗證它符合以下標準: 是ATLEAST 8個字符長
包含ATLEAST 1個下部字母字符
包含ATLEAST 1上字母字符
包含ATLEAST 1數字位
包含從集合,包括至少一個特殊字符:!@#$%^ & *
不包含「和」字或單詞「結束」
輸出提示用戶輸入一個字符串並在輸出中包含上述要求 。
輸出一個表示有效或無效的字符串。如果無效,說明上面的規則沒有得到滿足。
利用了以下功能:
的indexOf
循環結構
的charAt()
ISDIGIT()
isUpperCase()
isLowerCase()
和所需的任何附加功能
對我來說,棘手的部分是它必須返回所有缺少的東西。就像我輸入了一個表示密碼的密碼,它應該會回來告訴我「你錯過了一個大寫字母,一個數字和一個特殊字符」
我有一個開始,但我很困惑如何讓它回報給我。 這是我迄今爲止
/********************************************
This program will test a password for:
8 characters
1 upper case
1 lower case
1 numeric digit
1 special character from the set [email protected]#$%^&*
and make sure it doesn't contain AND or END
If the password complies, it will return a valid answer
If not, it will tell the user what they need to do.
*********************************************/
import java.util.Scanner;
public class YoungAmyProg5
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String input;
//input password
System.out.println("Enter a password that follows these rules:\n Is at least 8 characters long\n Contains at least 1 lower case letter\n Contains at least 1 upper case letter\n Contains at least 1 numeric digit\n Contains at least 1 special character from the set: ! @#$%^&*\n Does NOT contain the word "and" or the word "end": ")
input= in.nextLine();
//Put through string and reply
if
public static boolean isSecurePassword(String password) {
int lengthPassword = password.length();
if (lengthPassword >= 8) {
return false;
}
boolean hasUppercase = false; //uppercase
boolean hasLowecase = false; //lowercase
boolean hasDigit = false; //digit
int specialChar = input.indexOf('!', '@', '#', '$', '%', '^', '&', '*'); //special character
int word = input.indexOf ('and', 'end'); //and or end
for (int i = 0; i < lengthPassword; i++) {
char ch = password.charAt(i);
if (Character.isUpperCase(ch)) {
hasUppercase = true;
}
if (Character.isLowerCase(ch)) {
hasLowercase = true;
}
if (Character.isDigit(ch)) {
hasDigit = true;
}
if (specialChar>0) {
specialChar = true;
}
if (word>0) {
word = true;
}
你應該去'regex'。這將使你的任務變得簡單 – frunkad 2015-02-11 19:11:07
爲什麼不只是讓你的布爾標誌爲全局的,如果你需要它們在你的函數之外呢? – ChrisStillwell 2015-02-11 19:11:45
「我真的很困惑如何讓它回報給我。」你想回報什麼東西?另外,爲什麼在那裏有一個神奇的浮動'if'? – leigero 2015-02-11 19:15:03