这应该是一个从用户那里获取密码并根据各种参数进行检查的程序。
我遇到了do while循环的问题,它打印出来:
Please enter your password.
Error. Password is too short. Try again.
然后在选择选项
后打印出来1. Enter your own password for verification
程序应该允许用户在打印任何错误消息之前输入密码。
首次使用本网站。对此的任何帮助将不胜感激。
import java.security.SecureRandom;
import java.util.*;
public class CC_CA2
{
// Main Method
public static void main(String[] args)
{
Scanner keyboardIn = new Scanner(System.in); //Create Scanner object
String[] data = {"123456","password","12345","12345678","qwerty","123456789","1234","baseball","dragon","football","1234567","monkey","letmein","abc123","111111","mustang","access","shadow","master","michael"};
String correct;
int numLetterCheck = 0;
int option;
boolean found = false;
boolean check;
boolean digitFound = false;
boolean letterFound = false;
//Menu
System.out.println("Welcome to the password checker.");
System.out.println("Would you like to:");
System.out.println("1. Enter your own password for verification.");
System.out.println("2. Have an easy to remember random password generated for you.");
option = keyboardIn.nextInt();
if (option==1)
{
do
{
check = true;
found=false;
System.out.println("Please enter your password."); // get user password
correct = keyboardIn.nextLine();
if (correct.length()<8) // 1. check password > 8 characters
{
System.out.println("Error. Password is too short. Try again.");
check=false;
continue;
}
if (correct.contains(" ")) //2. check if password contains any spaces
{
System.out.println("Error. Password contains a space. Try again.");
check=false;
continue;
}
for(int i = 0; i < data.length; i++) // 3. check common passwords
{
if(data[i].equals(correct)) //if match found
{
found = true; //remember that it is found
}
}
if(found) //if found is true
{
System.out.println("Error. This is a common password. Try again.");
check=false;
continue;
}
for (char ch : correct.toCharArray()) // 4. Make sure password contains at least one letter and one number
{
if (Character.isDigit(ch))
{
digitFound = true;
}
if (Character.isLetter(ch))
{
letterFound = true;
}
if (digitFound && letterFound)
{
numLetterCheck = 0;
}
else
{
numLetterCheck = 1;
}
}
while (numLetterCheck == 1)
{
letterFound=false;
digitFound=false;
System.out.println("Error. Password must contain at least one letter and one number. Try again.");
correct = keyboardIn.nextLine();
}
}while(check == false);
}
}}