英文:
Writing a program to check password for certain things
问题
我正在编写一个程序,以检查用户提供的密码中是否包含特定值。我已经发布了下面的代码,我认为一切都很好,只是对于 if 语句中布尔检查的语法应该如何检查感到困惑。对任何帮助表示感谢!
public class PasswordValidation
{
public static void main(String[] args){
Scanner stdin = new Scanner(System.in);
System.out.println("Password: ");
String password = stdin.next();
int passwordLength = password.length();
String pCheck = "password";
boolean capital;
boolean lowercase;
boolean number;
boolean specialChar;
char check;
for(int i = 0; i < password.length(); i++){
check = password.charAt(i);
if(Character.isDigit(check)){
number = true;
}else if (Character.isUpperCase(check)){
capital = true;
}else if (Character.isLowerCase(check)){
lowercase = true;
}else{
specialChar = true;
}
}
if(number = true && capital = true && lowercase = true && specialChar = false && password.length() >= 8 && password.toLowerCase().contains(pCheck.toLowerCase())){
System.out.println("Password is good");
英文:
Im writing a program to check for values in a password given by the user. I have posted the code below and I think its all good I'm just confused on how the syntax for the boolean check in the if statement should be checked. Any help is appreciated, thanks!
public class PasswordValidation
{
public static void main(String[] args){
Scanner stdin = new Scanner(System.in);
System.out.println("Password: ");
String password = stdin.next();
int passwordLength = password.length();
String pCheck = "password";
boolean capital;
boolean lowercase;
boolean number;
boolean specialChar;
char check;
for(int i = 0; i < password.length(); i++){
check = password.charAt(i);
if(Character.isDigit(check)){
number = true;
}else if (Character.isUpperCase(check)){
capital = true;
}else if (Character.isLowerCase(check)){
lowercase = true;
}else{
specialChar = true;
}
}
if(number = true && capital = true && lowercase = true && specialChar = false && password.length() >= 8 && password.toLowerCase().contains(pCheck.toLowerCase())){
System.out.println("Password is good");
答案1
得分: 1
你有一些错误,要检查值的相等性,你需要使用 ==
而不是 =
number == true && capital == true && lowercase == true ...
或者更好的做法是
number && capital && lowercase ...
因为它们只是布尔值。
英文:
you have some errors, to check equality of value you need to use ==
not =
number == true && capital == true && lowercase == true ...
or better you can do this
number && capital && lowercase ...
because they are just booleans
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论