Do-While循环问题,再次运行提示-JAVA

时间:2015-10-05 01:50:48

标签: java loops

我试图让它成为如果用户输入'y'再次运行该程序。用户执行此操作时会发生的情况是程序再次运行,但不正确。这就是我到目前为止所拥有的。我知道while while循环最适合这个。变量初始化的错误是什么?

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 (!@#$%^&*()_+)\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 (!@#$%^&*()_+)

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)  

2 个答案:

答案 0 :(得分:0)

您需要在runAgain = scan.next();阅读完整的线路表格控制台。只需将单个令牌读取到runAgain,控制台将保留\r返回字符,该字符将作为下一个密码读取scan.nextLine()。您可以将语句更改为runAgain = scan.nextLine().trim();

答案 1 :(得分:0)

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