简体   繁体   English

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

[英]Do-While loop issue, making the prompt run again -JAVA

I am attempting to make it so this program runs again if the user enters 'y'. 我正在尝试制作该程序,以便在用户输入“ y”时再次运行该程序。 What happens when the user does this is the program runs again, but not correctly. 用户执行此操作时,将再次运行程序,但运行不正确。 This is what I have so far. 到目前为止,这就是我所拥有的。 I know a do while loop will be best for this. 我知道一个do while循环将是最好的选择。 Is it an error in how the variables are initialized? 变量的初始化方式有误吗?

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

which gives this output when yes is entered to run again. 输入yes再次运行时,将提供此输出。 It skips the prompt entirely. 它会完全跳过提示。

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)  

You would need to read the complete line form console at runAgain = scan.next(); 您需要在runAgain = scan.next();处阅读完整的行窗体控制台runAgain = scan.next(); . Just single token is being read to runAgain and the console is left with \\r or Return character which will be read as next password where you do scan.nextLine() . 仅单个令牌被读取到runAgain ,控制台上留下\\rReturn字符,这将在您执行scan.nextLine()作为下一个密码读取。 You may change the statement to runAgain = scan.nextLine().trim(); 您可以将语句更改为runAgain = scan.nextLine().trim(); .

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM