英文:
Trouble returning the correct boolean value
问题
import java.util.Scanner;
public class Password {
public static void main(String[] args) {
String another = "y";
while (another.equalsIgnoreCase("y")) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a password: ");
String password = scan.nextLine();
if (checkPassword(password)) {
System.out.println("Entered Password: " + password);
System.out.println("Judgement: " + "Valid password");
System.out.println("Re-run Program (y/n)? ");
another = scan.next();
} else {
System.out.println("Entered Password: " + password);
System.out.println("Judgement: " + "Invalid password");
System.out.println("Re-run Program (y/n)? ");
another = scan.next();
}
}
}
public static boolean checkPassword(String password) {
if (!minChars(password)) {
return false;
} else if (!onlyCharsAndDigits(password)) {
return false;
} else if (!minDigits(password)) {
return false;
} else if (!minLowerCase(password)) {
return false;
} else if (!minUpperCase(password)) {
return false;
}
return true;
}
public static boolean minChars(String password) {
if (password.length() > 8) {
return true;
} else {
return false;
}
}
public static boolean onlyCharsAndDigits(String password) {
for (int i = 0; i < password.length(); i++) {
if (!Character.isLetter(password.charAt(i)) && !Character.isDigit(password.charAt(i))) {
return false;
}
}
return true;
}
public static boolean minDigits(String password) {
int numberOfDigits = 0;
for (int i = 0; i < password.length(); i++) {
if (Character.isDigit(password.charAt(i))) {
numberOfDigits++;
}
}
return numberOfDigits >= 3;
}
public static boolean minLowerCase(String password) {
int numberOfLowerCase = 0;
for (int i = 0; i < password.length(); i++) {
if (Character.isLowerCase(password.charAt(i))) {
numberOfLowerCase++;
}
}
return numberOfLowerCase >= 3;
}
public static boolean minUpperCase(String password) {
int numberOfUpperCase = 0;
for (int i = 0; i < password.length(); i++) {
if (Character.isUpperCase(password.charAt(i))) {
numberOfUpperCase++;
}
}
return numberOfUpperCase >= 3;
}
}
英文:
I am trying to create a program to check a password. All 5 conditions must be true for the password to be valid. The problem I am having is if the entered password is long enough, the password is automatically valid even if the other conditions are not valid. I have tried changing many small things, but haven't been able to get it to check all the conditions. Any help is much appreciated!
public class Password
{
//Program main method
public static void main(String [] args)
{
String another = "y";
while (another.equalsIgnoreCase("y"))
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter a password: ");
String password = scan.nextLine();
//Print output
if (checkPassword(password))
{
System.out.println("Entered Password: " + password);
System.out.println("Judgement: " + "Valid password");
System.out.println("Re-run Program (y/n)? ");
another = scan.next();
}
else
{
System.out.println("Entered Password: " + password);
System.out.println("Judgement: " + "Invalid password");
System.out.println("Re-run Program (y/n)? ");
another = scan.next();
}
}
}
//
public static boolean checkPassword(String password)
{
if(!minChars(password))
{
return false;
}
else if (!onlyCharsAndDigits(password))
{
return false;
}
else if (!minDigits(password))
{
return false;
}
else if (!minLowerCase(password))
{
return false;
}
else if (!minUpperCase(password))
{
return false;
}
return true;
}
//
public static boolean minChars(String password)
{
if (password.length() > 8) {
return true;
}
else {
return false;
}
}
//method to test password has letters and digits or not
public static boolean onlyCharsAndDigits(String password)
{
for(int i=0;i < password.length();i++)
{
if(!Character.isLetter(password.charAt(i)) && !Character.isDigit(password.charAt(i)))
{
return false;
}
}
return true;
}
//method to test password has letters and digits or not
public static boolean minDigits(String password)
{
int numberOfDigits = 0;
for(int i=0;i < password.length();i++)
{
if(Character.isDigit(password.charAt(i)));
{
numberOfDigits++;
}
}
return numberOfDigits >= 3;
}
//method to test password has letters and digits or not
public static boolean minLowerCase(String password)
{
int numberOfLowerCase = 0;
for(int i=0;i < password.length();i++)
{
if(Character.isLowerCase(password.charAt(i)));
{
numberOfLowerCase++;
}
}
return numberOfLowerCase >= 3;
}
//method to test password has letters and digits or not
public static boolean minUpperCase(String password)
{
int numberOfUpperCase = 0;
for(int i=0;i < password.length();i++)
{
if(!Character.isUpperCase(password.charAt(i)));
{
numberOfUpperCase++;
}
}
return numberOfUpperCase >= 3;
}
}
答案1
得分: 2
将checkPassword
方法中的所有else if
更改为if
。
如果执行了任何前面的分支,else if
分支将不会被执行。
您的代码可以简化为
public static boolean checkPassword(String password) {
return minChars(password) && onlyCharsAndDigits(password)
&& minDigits(password) && minLowerCase(password) && minUpperCase(password);
}
英文:
Change all else if
to if
in the checkPassword
method.
An else if
branch will not be executed if any of the preceding branches are executed.
Your code can be simplified as
public static boolean checkPassword(String password) {
return minChars(password) && onlyCharsAndDigits(password)
&& minDigits(password) && minLowerCase(password) && minUpperCase(password);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论