Java密码程序循环永远需要帮助

时间:2011-02-03 18:42:38

标签: java

为什么即使我输入超过6个字符的长度,也会永远重复?

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");
        }
}

2 个答案:

答案 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 :(得分:1)

你有一些问题。第一个显然是你的while循环中的长度没有变化,其次enterPassword()实际上并没有改变密码。

此外,你无限调用调用checklength的enterpassword,调用调用的enterpassword .....不确定这是否是最好的习惯。

您应该尝试将您的功能分解为逻辑工作单元,以便它们更具可重用性。

enterpassword应该输入密码,然后checkpasswordlength应该独立检查长度,IMO

这件事怎么样?

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

}