检查密码,验证,并再次循环
Check password, verify, and loop again
我有一个问题需要至少 2 个大写字母、至少 3 个小写字母和 1 个数字。
这是确切的问题:
编写一个应用程序,提示用户输入至少包含两个大写字母、至少三个小写字母和至少一位数字的密码。不断提示用户,直到输入有效密码。如果密码有效则显示Valid password;如果不是,请显示密码无效的适当原因,如下所示:
例如,如果用户输入“密码”,您的程序应该输出:您的密码无效,原因如下:大写字母数字
如果用户输入“passWOrd12”,您的程序应该输出:valid password
到目前为止,这是我的编码,我遇到的问题是程序提示用户在输入密码后输入更多密码,但密码不正确。
import java.util.*;
public class ValidatePassword {
public static void main(String[] args) {
String inputPassword;
Scanner input = new Scanner(System.in);
System.out.print("Password: ");
inputPassword = input.next();
System.out.println(PassCheck(inputPassword));
System.out.println("");
}
public static String PassCheck(String Password) {
String result = "Valid Password";
int length = 0;
int numCount = 0;
int capCount = 0;
for (int x = 0; x < Password.length(); x++) {
if ((Password.charAt(x) >= 47 && Password.charAt(x) <= 58)
|| (Password.charAt(x) >= 64 && Password.charAt(x) <= 91)
|| (Password.charAt(x) >= 97 && Password.charAt(x) <= 122)) {
} else {
result = "Password Contains Invalid Character!";
}
if ((Password.charAt(x) > 47 && Password.charAt(x) < 58)) {
numCount++;
}
if ((Password.charAt(x) > 64 && Password.charAt(x) < 91)) {
capCount++;
}
length = (x + 1);
}
if (numCount < 2) {
result = "digits";
}
if (capCount < 2) {
result = "uppercase letters";
}
if (numCount < 2 && capCount < 2) {
result = "uppercase letters digits";
}
if (length < 2) {
result = "Password is Too Short!";
}
return (result);
}
}
你需要一个 while 循环。这将确保您的代码将一直循环直到成功。当它成功时,布尔值变为真并且它不会循环。在 while 循环之后写下你想要发生的任何事情。
public static void main(String[] args) {
String inputPassword;
Scanner input = new Scanner(System.in);
boolean success=false;
while(!success){
System.out.print("Password: ");
inputPassword = input.next();
System.out.println(PassCheck(inputPassword));
if(PassCheck(inputPassword).equals("Valid Password")) success = true;
System.out.println("");
}
}
您可以通过将 PassCheck
方法的 return 类型更改为布尔结果并使用 Character
class 检查大写小写数字字符和计数来实现最后出现每个检查条件,如果通过则结果为 true
否则为 false
并且您可以根据 PassCheck
的结果迭代 do-while
方法
public class ValidatePassword {
public static void main(String[] args) {
String inputPassword;
Scanner input = new Scanner(System.in);
boolean isValidPassword = false;
do {
System.out.print("Password: ");
inputPassword = input.next();
isValidPassword = PassCheck(inputPassword);
System.out.println("");
}while(!isValidPassword);
}
public static boolean PassCheck(String Password) {
boolean isValid = false;
String result = "";
int numCount = 0;
int capCount = 0;
int lowCount = 0;
for (int x = 0; x < Password.length(); x++) {
if(Character.isDigit(Password.charAt(x))) {
numCount++;
}
if(Character.isUpperCase(Password.charAt(x))) {
capCount++;
}
if(Character.isLowerCase(Password.charAt(x))) {
lowCount++;
}
}
if(numCount >= 1 && capCount >= 2 && lowCount >= 3) {
result = "Password Valid";
isValid = true;
} else {
isValid = false;
result = "Password is Invalid: ";
if(Password.length() < 2) {
result += " Password is Too Short!";
}
if(numCount < 1) {
result += " no digit";
}
if(capCount < 2) {
result += " at lease 2 Uppercase";
}
if(lowCount < 3) {
result += " at lease 3 Lowercase";
}
}
System.out.println(result);
return isValid;
}
}
下面是我在 Cengage 平台上运行的完成代码:
import java.util.*;
public class ValidatePassword {
public static void main(String[] args) {
String inputPassword;
Scanner input = new Scanner(System.in);
boolean success=false;
while(!success){
System.out.print("Password: ");
inputPassword = input.next();
System.out.println(PassCheck(inputPassword));
if(PassCheck(inputPassword).equals("Valid Password")) success = true;
System.out.println("");
}
}
public static String PassCheck(String Password) {
String result = "Valid Password";
int length = 0;
int numCount = 0;
int capCount = 0;
for (int x = 0; x < Password.length(); x++) {
if ((Password.charAt(x) >= 47 && Password.charAt(x) <= 58) || (Password.charAt(x) >= 64 && Password.charAt(x) <= 91) ||
(Password.charAt(x) >= 97 && Password.charAt(x) <= 122)) {
} else {
result = "Password Contains Invalid Character!";
}
if ((Password.charAt(x) > 47 && Password.charAt(x) < 58)) {
numCount++;
}
if ((Password.charAt(x) > 64 && Password.charAt(x) < 91)) {
capCount++;
}
length = (x + 1);
}
if (numCount < 2) {
result = "digits";
}
if (capCount < 2) {
result = "uppercase letters";
}
if (capCount < 2) {
result = "uppercase letters";
}
if (numCount < 2 && capCount < 2)
{
result = "uppercase letters digits";
}
if (length < 2) {
result = "Password is Too Short!";
}
return (result);
}
}
我有一个问题需要至少 2 个大写字母、至少 3 个小写字母和 1 个数字。
这是确切的问题:
编写一个应用程序,提示用户输入至少包含两个大写字母、至少三个小写字母和至少一位数字的密码。不断提示用户,直到输入有效密码。如果密码有效则显示Valid password;如果不是,请显示密码无效的适当原因,如下所示:
例如,如果用户输入“密码”,您的程序应该输出:您的密码无效,原因如下:大写字母数字
如果用户输入“passWOrd12”,您的程序应该输出:valid password
到目前为止,这是我的编码,我遇到的问题是程序提示用户在输入密码后输入更多密码,但密码不正确。
import java.util.*;
public class ValidatePassword {
public static void main(String[] args) {
String inputPassword;
Scanner input = new Scanner(System.in);
System.out.print("Password: ");
inputPassword = input.next();
System.out.println(PassCheck(inputPassword));
System.out.println("");
}
public static String PassCheck(String Password) {
String result = "Valid Password";
int length = 0;
int numCount = 0;
int capCount = 0;
for (int x = 0; x < Password.length(); x++) {
if ((Password.charAt(x) >= 47 && Password.charAt(x) <= 58)
|| (Password.charAt(x) >= 64 && Password.charAt(x) <= 91)
|| (Password.charAt(x) >= 97 && Password.charAt(x) <= 122)) {
} else {
result = "Password Contains Invalid Character!";
}
if ((Password.charAt(x) > 47 && Password.charAt(x) < 58)) {
numCount++;
}
if ((Password.charAt(x) > 64 && Password.charAt(x) < 91)) {
capCount++;
}
length = (x + 1);
}
if (numCount < 2) {
result = "digits";
}
if (capCount < 2) {
result = "uppercase letters";
}
if (numCount < 2 && capCount < 2) {
result = "uppercase letters digits";
}
if (length < 2) {
result = "Password is Too Short!";
}
return (result);
}
}
你需要一个 while 循环。这将确保您的代码将一直循环直到成功。当它成功时,布尔值变为真并且它不会循环。在 while 循环之后写下你想要发生的任何事情。
public static void main(String[] args) {
String inputPassword;
Scanner input = new Scanner(System.in);
boolean success=false;
while(!success){
System.out.print("Password: ");
inputPassword = input.next();
System.out.println(PassCheck(inputPassword));
if(PassCheck(inputPassword).equals("Valid Password")) success = true;
System.out.println("");
}
}
您可以通过将 PassCheck
方法的 return 类型更改为布尔结果并使用 Character
class 检查大写小写数字字符和计数来实现最后出现每个检查条件,如果通过则结果为 true
否则为 false
并且您可以根据 PassCheck
的结果迭代 do-while
方法
public class ValidatePassword {
public static void main(String[] args) {
String inputPassword;
Scanner input = new Scanner(System.in);
boolean isValidPassword = false;
do {
System.out.print("Password: ");
inputPassword = input.next();
isValidPassword = PassCheck(inputPassword);
System.out.println("");
}while(!isValidPassword);
}
public static boolean PassCheck(String Password) {
boolean isValid = false;
String result = "";
int numCount = 0;
int capCount = 0;
int lowCount = 0;
for (int x = 0; x < Password.length(); x++) {
if(Character.isDigit(Password.charAt(x))) {
numCount++;
}
if(Character.isUpperCase(Password.charAt(x))) {
capCount++;
}
if(Character.isLowerCase(Password.charAt(x))) {
lowCount++;
}
}
if(numCount >= 1 && capCount >= 2 && lowCount >= 3) {
result = "Password Valid";
isValid = true;
} else {
isValid = false;
result = "Password is Invalid: ";
if(Password.length() < 2) {
result += " Password is Too Short!";
}
if(numCount < 1) {
result += " no digit";
}
if(capCount < 2) {
result += " at lease 2 Uppercase";
}
if(lowCount < 3) {
result += " at lease 3 Lowercase";
}
}
System.out.println(result);
return isValid;
}
}
下面是我在 Cengage 平台上运行的完成代码:
import java.util.*;
public class ValidatePassword {
public static void main(String[] args) {
String inputPassword;
Scanner input = new Scanner(System.in);
boolean success=false;
while(!success){
System.out.print("Password: ");
inputPassword = input.next();
System.out.println(PassCheck(inputPassword));
if(PassCheck(inputPassword).equals("Valid Password")) success = true;
System.out.println("");
}
}
public static String PassCheck(String Password) {
String result = "Valid Password";
int length = 0;
int numCount = 0;
int capCount = 0;
for (int x = 0; x < Password.length(); x++) {
if ((Password.charAt(x) >= 47 && Password.charAt(x) <= 58) || (Password.charAt(x) >= 64 && Password.charAt(x) <= 91) ||
(Password.charAt(x) >= 97 && Password.charAt(x) <= 122)) {
} else {
result = "Password Contains Invalid Character!";
}
if ((Password.charAt(x) > 47 && Password.charAt(x) < 58)) {
numCount++;
}
if ((Password.charAt(x) > 64 && Password.charAt(x) < 91)) {
capCount++;
}
length = (x + 1);
}
if (numCount < 2) {
result = "digits";
}
if (capCount < 2) {
result = "uppercase letters";
}
if (capCount < 2) {
result = "uppercase letters";
}
if (numCount < 2 && capCount < 2)
{
result = "uppercase letters digits";
}
if (length < 2) {
result = "Password is Too Short!";
}
return (result);
}
}