每当我输入密码时,无论是有效格式还是格式无效,它都会输出"密码无效"。
import java.util.Scanner;
public class PasswordTest
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
boolean length = true;
boolean digit = true;
boolean lowercase = true;
boolean uppercase = true;
char ch = 0;
String s1;
//Prompt user to enter password
System.out.print("Enter password: ");
s1 = input.nextLine();
//Check what ch is
for (int i=0; i<s1.length(); i++){
ch = s1.charAt(i);
if (Character.isDigit(ch)){
digit = true;
}
if (Character.isLowerCase(ch)){
lowercase = true;
}
if (Character.isUpperCase(ch)){
uppercase = true;
}
if (s1.length()>=8){
length = true;
}
}
if (digit==false && lowercase==false && uppercase==false && length==false)
System.out.println("Valid password");
else
System.out.println("Invalid password");
}
}
答案 0 :(得分:1)
您需要在声明时首先将所有布尔值设置为false
,然后在下面的代码中将其设置为true
,只要它满足条件。同时打印“有效密码”时,检查所有布尔值是否为true
,否则打印“无效密码”。检查字符串长度是否大于8应该在循环体外。以下代码有效。
import java.util.Scanner;
public class PasswordTest
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
boolean length = false;
boolean digit = false;
boolean lowercase = false;
boolean uppercase = false;
char ch=0;
String s1;
//Prompt user to enter password
System.out.print("Enter password: ");
s1 = input.nextLine();
//Check what ch is
for (int i=0; i<s1.length(); i++){
ch = s1.charAt(i);
if (Character.isDigit(ch)){
digit = true;
}
if (Character.isLowerCase(ch)){
lowercase = true;
}
if (Character.isUpperCase(ch)){
uppercase = true;
}
}
if (s1.length()>=8){
length = true;
}
if (digit==true && lowercase==true && uppercase==true && length==true)
System.out.println("Valid password");
else
System.out.println("Invalid password");
}
}
答案 1 :(得分:0)
我有一个问题,如果它不能是小写的,如果它不能大写那么它会是什么?我做了它所以它必须是小写的,这里是代码(如果你想要全部大写,那么将代码if(digit[i] == true || lowercase[i] == false || uppercase[i] == true)
中的if语句更改为if(digit[i] == true || lowercase[i] == true || uppercase[i] == false)
import java.util.Scanner;
public class PasswordTest{
public static void main (String[] args){
Scanner input = new Scanner(System.in);
boolean length;
boolean pass = true;
String s1;
//Prompt user to enter password
System.out.print("Enter password: ");
s1 = input.nextLine();
boolean[] digit = new boolean[s1.length()];
boolean[] lowercase = new boolean[s1.length()];
boolean[] uppercase = new boolean[s1.length()];
char[] ch = new char[s1.length()];
//Check what ch is
for (int i=0; i<s1.length(); i++){
ch[i] = s1.charAt(i);
digit[i] = Character.isDigit(ch[i]);
lowercase[i] = Character.isLowerCase(ch[i]);
uppercase[i] = Character.isUpperCase(ch[i]);
}
length = s1.length()>=8;
if (length==false && s1.length() > 0) {
int i;
for(i = 0; i < s1.length(); i ++){
if(digit[i] == true|| lowercase[i] == false|| uppercase[i] == true)
pass = false;
}
if(pass == true)
System.out.println("Valid password");
else{
System.out.println("Invalid password");
}
}else{
System.out.println("Invalid password");
}
}
}
答案 2 :(得分:0)
你所做的是首先将布尔值设置为true然后再次在循环中将其设置为true,这没有意义。我认为此代码可以正常工作
Scanner input = new Scanner(System.in);
boolean length = false;
boolean digit = false;
boolean lowercase = false;
boolean uppercase = false;
char ch = '\u0000';
String s1;
//Prompt user to enter password
System.out.print("Enter password: ");
s1 = input.nextLine();
//Check what ch is
for (int i=0; i<s1.length(); i++){
ch = s1.charAt(i);
if (Character.isDigit(ch)){
digit = true;
}
if (Character.isLowerCase(ch)){
lowercase = true;
}
if (Character.isUpperCase(ch)){
uppercase = true;
}
if (s1.length()>=8){
length = true;
}
}
if (digit==true && lowercase==true && uppercase==true && length==true)
System.out.println("Valid password");
else
System.out.println("Invalid password");
答案 3 :(得分:0)
对于未来,我认为regular expressions会有所帮助。
您的密码要求似乎是&#34;长度为8个或更多字符,其中每个字符可以是小写字母,大写字母或数字,&#34;是对的吗?我们可以使用正则表达式表达这些要求,例如([a-z]|[A-Z]|[0-9]){8,}
。有了这个,我们可以写
String password = input.nextLine();
if(password.matches("([a-z]|[A-Z]|[0-9]){8,}") {
// Go about your business.
}