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

all*_*ded 1 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");
        }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 10

    length = password.length();
    while (length < 6){
        enterPassword();
    }
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)