2011-02-03 17 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

あなたが何かのようにユーザー名を格納していませんか? –

答えて

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()は、checkPasswordLength()を呼び出すenterPassword()を呼び出します。プログラムは非常に簡単な流れで書くことができました! – cheekoo

+0

@cheekoo:そうです。私はあなたがそれを言ったのと同時に私の答えにそれを編集していました。検証に副作用ロジックを挿入するのではなく、メソッドに1つの責任を持たせるのが最善です。 –

+0

それは本当にうまくいく素晴らしい.1つの質問。私の本は私を教えていないが(!xxxxx(xxx))それは何と呼ばれるのですか?私はあなたが真実ではないと言っていることを実感します。私はより深い理解を得るためにそれをもっと深く見ていきたいと思います。 – allencoded

1

あなたはいくつかの問題を持っています。最初は明らかに、あなたのwhileループで長さが変わらないことです。次にenterPassword()は実際にパスワードを変更しません。

また、enterpasswordを呼び出すenterpasswordを呼び出すchecklengthを無限に呼び出しています。これは、これが呼び出すenterpasswordを呼び出します。これが最善の習慣であるかどうかはわかりません。

機能を再利用可能にするために、機能を論​​理作業単位に分解してください。

ENTERPASSWORDは、checkpasswordlengthがIMO

どのようにこのliek何かについて、independantly長さをチェックする必要があり、パスワードを入力する必要がありますか?

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.... 
    } 

}

+0

助けてくれてありがとう。誰かに電話して別の人に電話してもらうということは、私がこの習慣から抜け出すために働くことを意味しています。 – allencoded

関連する問題