2011-02-03 71 views
1

爲什麼這只是一直重複,即使當我輸入超過6個字符的長度?Java密碼程序循環永遠需要幫助

import java.util.Scanner; 

class Password { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     System.out.println("Welcome please enter your username and password."); 
     System.out.print("Username >>"); 
     input.nextLine(); 
     enterPassword(); 
     System.out.println("Successfully Logged In"); 
     } 

    public static void enterPassword(){ 
     String password; 
     Scanner input = new Scanner(System.in); 
     System.out.print("Password >>"); 
     password = input.nextLine(); 
     checkPasswordLength(password); 
     } 


    public static void checkPasswordLength(String password){ 
     int length; 
     length = password.length(); 
     while (length <6){ 
      enterPassword(); 
      } 
     checkPasswordLetter(password); 
     } 

    public static void checkPasswordLetter(String password){ 
     System.out.println("More checking here to be added"); 
     } 
} 
+0

你不存儲你的用戶名是什麼? – 2013-07-02 03:29:19

回答

10
length = password.length(); 
    while (length < 6){ 
     enterPassword(); 
    } 

你永遠不更新length,甚至獲得新的密碼後。

這裏有一個更好的方式來組織你的代碼:

public static String enterPassword() { 
    //gets a string and returns it 
} 

public static boolean checkPasswordLength(String password) { 
    //if too long return false 
} 

//... 
String password = enterPassword(); 
while (!checkPasswordLength(password)) { 
    password = enterPassword(); 
} 
+1

你是對的,那個長度在循環內不會更新。但我覺得這個流程很腥。 checkPasswordLength()調用enterPassword(),然後調用checkPasswordLength()。該程序可以用一個非常簡單的流程編寫! – cheekoo 2011-02-03 18:48:54

+0

@cheekoo:你是對的;我是在你說的同時把它編輯成我的答案的。最好有一個方法有一個責任,而不是在驗證中插入副作用邏輯。 – 2011-02-03 18:50:05

1

你有幾個問題。第一個顯然是你的while循環長度不變,其次enterPassword()實際上並不改變密碼。

此外,你無限地調用enterpassword調用checklength調用enterpassword調用.....不知道這是否是最好的習慣。

您應該嘗試將您的功能分解爲邏輯工作單元,以便它們更具可重用性。

enterpassword應輸入密碼,然後checkpasswordlength應該independantly檢查長度,IMO

怎麼樣東西liek呢?

import java.util.Scanner; 

class Password { 
public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 

    System.out.println("Welcome please enter your username and password."); 
    System.out.print("Username >>"); 
    input.nextLine(); 

    String password = ""; 

    //keep going until we get an acceptable password 
    while(!CheckPassword(password)) 
    {  
    password = enterPassword(); 
    } 
    System.out.println("Successfully Logged In"); 
    } 


    public static Boolean CheckPassword(String password) 
    { 
    //perform all password checks 
    Boolean passedLength = checkPasswordLength(password); 
    Boolean passedLetter = checkPasswordLetter(password); 
    return (passedLength && passedLetter);     

    } 

public static String enterPassword(){ 
    String password; 
    Scanner input = new Scanner(System.in); 
    System.out.print("Password >>"); 
    password = input.nextLine();  


    return password; 
    } 


public static Boolean checkPasswordLength(String password){ 
    //passes if there is a string value, and it has 6+ characters 
    return (password != null && password.length >=6);   
    } 

public static Boolean checkPasswordLetter(String password){ 
    System.out.println("More checking here to be added"); 
    return true; //for now.... 
    } 

}