我想要做的是从用户那里获取密码,然后检查它的 4 个条件。如果是对的。然后打印出“密码正确”。如果不是,请向用户显示问题所在并返回菜单。问题是我无法返回菜单。如果我在 line 中做一个 while 循环//User interface
//suppose to put a while loop here, but.....
System.out.println();
System.out.println("Please create a password and a legal one"
。它确实会返回,但某些“if”语句不会运行。谁能看到我被困在哪里?谢谢你。
import java.util.Scanner;
public class Text_processing
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in); //user input
String user_input; // To hold input
char[] array; //Array for user_input
int letters = 0; // Number of letters
int digits = 0; // Number of digits
boolean nu = true; //
boolean nl = true;
boolean nd = true;
boolean n7 = true;
//User interface
//suppose to put a while loop here, but.....
System.out.println();
System.out.println("Please create a password and a legal one"
+ " should contain the following elements:");
System.out.println("\tAt least 7 characters in length.");
System.out.println("\tAt least 1 upper case letter.");
System.out.println("\tAt least 1 lower case letter.");
System.out.println("\tAt least 1 number.");
//Get a string from the user
user_input = kb.nextLine();
//Convert it to a char array
array = user_input.toCharArray();
for(int i = 0; i < array.length; i++)
{
if(!nu && !nl && !nd && !n7)
{
break;
}
if (array.length >= 7) //not < 7 cos breaks
{
n7 = false;
}
if(Character.isUpperCase(array[i]))
{
nu = false;
}
if (Character.isLowerCase(array[i]))
{
nl = false;
}
if (Character.isDigit(array[i]))
{
nd = false;
}
}
if(n7)
{
System.out.println("Password does not contain 7 or more letters.");
}
if(nu)
{
System.out.println("Password does not contain an upper case letter.");
}
if(nl)
{
System.out.println("Password does not contain an lower case letter.");
}
if(nd)
{
System.out.println("Password does not contain a number.");
}
System.out.println("Password is correct.");
}
}