2015-10-05 29 views
0

我正在嘗試使該程序再次運行,如果用戶輸入'y'。當用戶執行此操作時會發生什麼情況,程序會再次運行,但不正確。這是我迄今爲止所擁有的。我知道一個while while循環對於這個最好。這些變量是如何初始化的錯誤?Do-While循環問題,使提示再次運行-JAVA

String password; 
    boolean hasLength; 
    boolean hasUppercase; 
    boolean hasLowercase; 
    boolean hasDigit; 
    boolean hasSpecial; 
    String runAgain = ""; 

    Scanner scan = new Scanner(System.in); 
    /****************************************************************************** 
    *        Inputs Section         * 
    ******************************************************************************/ 

    do { 

    System.out.println("A password must be at least 8 character long"); 
    System.out.println("And must contain:"); 
    System.out.println("-At least 1 number"); 
    System.out.println("-At least 1 uppercase letter"); 
    System.out.println("-At least 1 special character ([email protected]#$%^&*()_+)\n"); 
    System.out.print("Please enter your new password: "); 
    password = scan.nextLine(); 

    /****************************************************************************** 
    *       Processing Section        * 
    ******************************************************************************/ 
    System.out.print("\n"); 
    System.out.println("Entered Password:\t" + password); 

    hasLength = password.length() < 8; // parameters for length 
    // for lower and uppercase characters 
    hasUppercase = !password.equals(password.toLowerCase()); //lowercase version of the password equals the password, 
    hasLowercase = !password.equals(password.toUpperCase()); //only if it does not contain uppercase letters. 
    hasDigit = password.matches(".*[0-9].*");//checks for digits 
    hasSpecial = !password.matches("[A-Za-z0-9]*"); //for anything not a letter in the ABC's 
    //prints the verdict 
    System.out.print("Verdict: "); 
    if(hasLength) 
    { 
     System.out.println("\t\tInvalid, Must have at least 8 characters"); 
    } 

    if(!hasUppercase) 
    { 
     System.out.println("\t\t\tInvalid, Must have an uppercase character"); 
    } 
    if(!hasLowercase) 
    { 
     System.out.println("\t\t\tInvalid, Must have a lowercase character"); 
    } 
    if(!hasDigit) 
    { 
     System.out.println("\t\t\tInvalid, Must have a number"); 
    } 
    if(!hasSpecial) 
    { 
     System.out.println("\t\t\tInvalid, Must have a special character"); 
    } 
    System.out.print("Would you like to make another password? (Y/N) "); 
    runAgain = scan.next(); 
    System.out.println("\n"); 

    } while (runAgain.equalsIgnoreCase("Y")); 

當yes被輸入以再次運行時,它給出這個輸出。它完全跳過提示。

Would you like to make another password? (Y/N) y 


A password must be at least 8 character long 
And must contain: 
-At least 1 number 
-At least 1 uppercase letter 
-At least 1 special character ([email protected]#$%^&*()_+) 

Please enter your new password: 
Entered Password: 
Verdict:  Invalid, Must have at least 8 characters 
       Invalid, Must have an uppercase Character 
       Invalid, Must have a lowercase character 
       Invalid, Must have a number 
       Invalid, Must have a special character 
Would you like to make another password? (Y/N) 
+0

你應該看看http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods –

+1

你是什麼意思「不正確「? – JVon

回答

0

您需要閱讀完整的線表格控制檯runAgain = scan.next();。只有單個令牌正在讀取到runAgain並且控制檯保留爲\r返回將在下一個密碼中讀取的字符scan.nextLine()。您可以將聲明更改爲runAgain = scan.nextLine().trim();

0
System.out.print("Would you like to make another password? (Y/N) "); 
    // runAgain = scan.next(); 
runAgain = scan.nextLine(); 
    System.out.println("\n");