Sự cố mà tôi gặp phải là tôi không thể lấy Chương trình Xác minh Mật khẩu của mình để kiểm tra chuỗi để đảm bảo rằng, 1 ký tự nằm trong chữ hoa và một là trong trường hợp thấp hơn, nó sẽ kiểm tra toàn bộ chuỗi cho một trong những khác và in thông báo lỗi dựa trên đó tuyên bố nó đang kiểm tra.Kiểm tra trường hợp chữ cái (Trên/Dưới) trong một chuỗi trong Java
Tôi đã xem xét trang web này và internet để tìm câu trả lời và tôi không thể tìm thấy câu trả lời. Đây là bài tập về nhà.
Dưới đây là mã hiện tại của tôi.
import java.util.Scanner;
public class password
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String password;
String cont = "y";
char ch;
boolean upper = false;
boolean lower = false;
System.out.println("Setting up your password is easy. To view requirements enter Help.");
System.out.print("Enter password or help: ");
password = stdIn.next();
ch = password.charAt(0);
while (cont.equalsIgnoreCase("y"))
{
while (password.isEmpty())
{
System.out.print("Enter password or help: ");
password = stdIn.next();
}
if (password.equalsIgnoreCase("help"))
{
System.out.println("Password must meet these requirements." +
"\nMust contain 8 characters.\nMust contain 1 lower case letter." +
"\nMust contain 1 upper case letter.\nMust contain 1 numeric digit." +
"\nMust contain 1 special character [email protected]#$%^&*\nDoes not contain the word AND or NOT.");
password = "";
}
else if (password.length() < 8)
{
System.out.println("Invalid password - Must contain 8 charaters.");
password = "";
}
else if (!(Character.isLowerCase(ch)))
{
for (int i=1; i<password.length(); i++)
{
ch = password.charAt(i);
if (!Character.isLowerCase(ch))
{
System.out.println("Invalid password - Must have a Lower Case character.");
password = "";
}
}
}
else if (!(Character.isUpperCase(ch)))
{
for (int i=0; i<password.length(); i++)
{
ch = password.charAt(i);
if (!Character.isUpperCase(ch))
{
System.out.println("Invalid password - Must have an Upper Case character.");
password = "";
}
}
}
else
{
System.out.println("Your password is " + password);
System.out.print("Would you like to change your password? Y/N: ");
cont = stdIn.next();
password = "";
}
while (!cont.equalsIgnoreCase("y") && !cont.equalsIgnoreCase("n"))
{
System.out.print("Invalid Answer. Please enter Y or N: ");
cont = stdIn.next();
}
}
}
}
Tôi tưởng tượng bạn muốn áp dụng * tất cả * của những hạn chế này vào mật khẩu, và else if không phải là xây dựng cho điều đó. –