2016-11-17 124 views
-2

我目前正在研究我的Java類的最終項目,我需要一點幫助!所以首先我要告訴你的指示,我的代碼,然後是我的問題。密碼驗證程序Java

說明

  1. 編寫提示用戶輸入密碼的程序。
  2. 創建一個名爲valid的布爾變量並將其設置爲true。如果以下任何測試失敗,請將其設置爲true。
  3. 檢查密碼,看它是否至少有8個字符。如果沒有,則顯示消息「密碼必須至少包含8個字符」
  4. 檢查密碼以查看它是否僅包含字母和數字。爲此,您需要遍歷字符串中的所有字符。角色C是數字信如果此表達式爲真:

    ( 'A' < = C & &Ç< = 'Z')|| ('A'< = c & & c < ='Z')|| ( '0' < = C & &Ç< = '9')

,如果這是即使不是真的,從你的循環打破並顯示信息, 「密碼必須只包含字母和數字」 5.如果程序結束時仍然有效,請顯示消息「密碼已接受!」。

我的代碼

import java.util.Scanner; 
public class PasswordVerification { 

    public static void main(String[] args) { 
//  Creates a scanner 
     Scanner sc = new Scanner(System.in); 
     boolean valid = false; 
     String password; 


//  Asks user to enter password 
     System.out.print("Please enter password and then hit enter:"); 
     password = sc.nextLine(); 

//  Checks to see if password is at least 8 characters. 
     if (password.length()<8) 
      { 
       valid = false; 
       System.out.println("Password must have at least 8 characters"); 
      } 

//  Checks each character to see if it is acceptable. 
     for (int i = 0; i < password.length(); i++){ 
        char c = password.charAt(i); 

        if (  ('a' <= c && c <= 'z') // Checks if it is a lower case letter 
          || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter 
          || ('0' <= c && c <= '9') //Checks to see if it is a digit 
        ) 
        { 

         valid = true; 
        } 

        else 
        { 
         // tells the user that only letters & digits are allowed 
         System.out.println("Only letter & digits are acceptable."); 
         valid = false; 
         break; 
        } 

     } 

     // if the password is valid, tell the user it's accepted 
     System.out.println("Password Accepted"); 
     } 


} 

因此,有說明書和我的代碼,我到目前爲止。我接近完成我只是在第四部分遇到問題。因此,預期輸出應該是:

Please enter a password: abc 
Password much have at least 8 characters 
Please enter a password: abcd1234$ 
Password must only contain letter and digits 
Please enter a password: #### 
Password must have at least 8 characters 
Password must only contain letters and digits 
Please enter a password: abcd1234 
Password accepted! 

當我鍵入ABC,這是我所得到的:

Please enter password and then hit enter:abc 
Password must have at least 8 characters 
Password Accepted 

當我做這個節目結束!有人可以幫我解決這個問題嗎?先謝謝了。 =)另外,這是一個Java編程I類。

+1

正則表達式可能是第4部分非常有幫助。有沒有理由不使用它?示例:http://stackoverflow.com/questions/11241690/regex-for-checking-if-a-string-is-strictly-alphanumeric – Joe

+2

你需要添加一個循環,一個ex爲ex,並且所有的時間都在那裏直到密碼有效 – cralfaro

+0

@joe海報顯然是一個完整的新手。正則表達式是稍後要解決的問題。 – slim

回答

2

由於@cralfaro說你必須重複這個過程,如果密碼是無效的:

import java.util.Scanner; 
public class PasswordVerification { 

    public static void main(String[] args) { 
     //  Creates a scanner 
     Scanner sc = new Scanner(System.in); 
     boolean valid = false; 
     String password; 

     do { // start a loop 
      //  Asks user to enter password 
      System.out.print("Please enter password and then hit enter:"); 
      password = sc.nextLine(); 

      //  Checks to see if password is at least 8 characters. 
      if (password.length()<8) 
      { 
       valid = false; 
       System.out.println("Password must have at least 8 characters"); 
       continue; // skip to next iteration 
      } 

      //  Checks each character to see if it is acceptable. 
      for (int i = 0; i < password.length(); i++){ 
       char c = password.charAt(i); 

       if (  ('a' <= c && c <= 'z') // Checks if it is a lower case letter 
         || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter 
         || ('0' <= c && c <= '9') //Checks to see if it is a digit 
       ) 
       { 

        valid = true; 
       } 
       else 
       { 
        // tells the user that only letters & digits are allowed 
        System.out.println("Only letter & digits are acceptable."); 
        valid = false; 
        break; 
       } 

      } 
     } while(!valid); // verify if the password is valid, if not repeat the process 

     // if the password is valid, tell the user it's accepted 
     System.out.println("Password Accepted"); 
    } 


} 

這樣的程序會繼續要求用戶輸入,如果密碼是無效的。

編輯: 感謝GC_的評論,問題是我錯過了第一次檢查中的繼續聲明。

+1

應該在第一次檢查時繼續。 –

+0

我試過你的代碼,它仍然不會運行,它應該.......程序終止密碼是否無效。但我認爲它應該工作!我錯過了什麼嗎? – BlazeRyder

+0

@GC_感謝您的評論,繼續它應該工作,現在編輯! – aleb2000

-1

在打印密碼被接受之前,檢查密碼是否仍然有效。

if(valid==true) { 
    System.out.println("Password Accepted"); 
} 

編輯:你也可以添加這個。

else { 
    System.out.println("Password Denied"); 
} 
+0

爲什麼選擇投票? –

+0

我剛要問這個!請解釋downvotes。我真的相信這是正確的答案。 – Joe

+0

哦,他確實希望它在一個循環。此外,它應該是有效的=有效&& true; –

0

你的問題IST這部分代碼:

if ( ('a' <= c && c <= 'z') // Checks if it is a lower case letter 
    || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter 
    || ('0' <= c && c <= '9') //Checks to see if it is a digit 
    ) { valid = true; } 

在這裏,您重置validtrue,即使是與長度的第一檢查小於8已經失敗。 因此abc將失敗3.和打印Password must have at least 8 characters這是好的。然後它會通過4.,重置有效到true和打印Password Accepted

所以,你要通過

if (!(// if the character is none of the options below, print error 
     ('a' <= c && c <= 'z') // Checks if it is a lower case letter 
    || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter 
    || ('0' <= c && c <= '9') //Checks to see if it is a digit 
    )) { 
     // tells the user that only letters & digits are allowed 
     System.out.println("Only letter & digits are acceptable."); 
     valid = false; 
     break; 
    } 

編輯的更換if (...) { valid = true; } else { ... }部分: 你也應該首先設置boolean valid = true;代替false,只要你想將其設置爲false,只有當它失敗的條件。 然後在你的代碼的末尾,添加一個條件檢查周圍的最後輸出線valid,像

if(valid) { 
    System.out.println("Password Accepted"); 
} 
+0

當我把它放到我的代碼中,運行它,並把abc作爲密碼,這是我得到的:請輸入密碼,然後按回車:abc 密碼必須至少有8個字符 密碼接受! – BlazeRyder

+0

對不起,我錯過了那部分,我編輯了我的解決方案 – Aracurunir

0

有點混亂,但試試這個:

public static void main(String[] args) { 
//  Creates a scanner 
Scanner sc = new Scanner(System.in); 
boolean valid = false; 
String password; 


//  Asks user to enter password 
while(true){ 
System.out.print("Please enter password and then hit enter:"); 
password = sc.nextLine(); 

//  Checks to see if password is at least 8 characters. 
if (password.length()<8) 
    { 
     valid = false; 
     System.out.println("Password must have at least 8 characters"); 
    } 
else { 

//  Checks each character to see if it is acceptable. 
for (int i = 0; i < password.length(); i++){ 
      char c = password.charAt(i); 

      if (  ('a' <= c && c <= 'z') // Checks if it is a lower case letter 
        || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter 
        || ('0' <= c && c <= '9') //Checks to see if it is a digit 
      ) { 
       valid = true; 
      } else { 
       System.out.println("Password denied"); 
       System.out.println("Only letter & digits are acceptable."); 
       valid = false; 
       break; 
      } 


} 

if (valid == true) { 
System.out.println("Password accepted"); 
    break; 
     }    
} 
} 
} 
+0

這似乎與循環工作,但它不起作用。當我鍵入abc它按預期工作,但是當我鍵入abcd1234 $它說密碼接受。但是,情況並非如此,因爲只有在它有8個字符(它會這樣做)並且所有字符都是字母和數字(這是$的錯誤)之後才能被接受。這是eclipse中的輸出:請輸入密碼並然後按回車鍵:abc 密碼必須至少有8個字符 請輸入密碼,然後按回車:abcd1234 $ 密碼接受 – BlazeRyder

+0

已更新它。再試一次 – Cristophs0n