0

嘿,我正在创建一个密码验证程序,该程序要求程序检查密码的数字、字母和长度,然后程序比较两个密码以查看它们是否匹配。一切正常,但是当显示错误消息时,它会显示多个消息框。我知道这与 for 循环有关,但我仍然是初学者,我不知道如何修复它。

这是我的代码:

import javax.swing.JOptionPane;

public class Passwords
{
public static void main(String[] args)
{
    String passOne, passTwo;
    passOne = JOptionPane.showInputDialog(null, "Please enter a password");
    passTwo = JOptionPane.showInputDialog(null, "Please re-enter your password");




    //Loop for each digit in password
    for(int x = 0; x < passOne.length(); x++)
    {
        //Testing for a digit
        if(Character.isDigit(passOne.charAt(x)))
            {
                //Testing for a letter
                if(Character.isLetter(passOne.charAt(x)))
                {
                    //Testing length 6-10 chars
                    if(passOne.length() <= 10 && passOne.length() >= 6)
                    {
                        //comparing two passwords
                        if(passOne.equals(passTwo))
                        {
                            JOptionPane.showMessageDialog(null, "Contratulations, you have a new password!");
                        }
                        //If passwords don't match
                        else
                        {
                            JOptionPane.showMessageDialog(null, "Passwords do not match, please try again.");
                        }
                    }
                    //If length is wrong
                    else
                    {
                        JOptionPane.showMessageDialog(null, "Password must be between 6 and 10 characters long.");
                    }
                }
                //If no letter
                else
                {

                    JOptionPane.showMessageDialog(null, "Password must contain at least one letter.");
                }

            }
        //If no digit
        else
            {

                JOptionPane.showMessageDialog(null, "Password must contain at least one digit.");           
            }


    }


}
}
4

4 回答 4

1

您的代码有几个问题

  1. 您正在迭代密码的字符,但在每次迭代中,您都会检查整个密码的长度。您只需要在循环之外执行一次。这也适用于检查passOnepassTwo

  2. “外包”代码以检查数字/字符,如下所示:

    static boolean containsOnlyDigitsOrLetters(String s)
    {
        for(int i = 0; i < s.length; i++)
        {
               if(!(Character.isLetter(s.charAt(i)) || Character.isDigit [...]))
               {
                   // if the current character is neither a letter nor a digit
                   return false;
               }
        }
        // all characters are either digits or letters
        return true;
    }
    

    我故意发布了“仅包含数字和字母”的代码,而不是“至少一个数字”/“至少一个字母”顺便说一句;)然后你可以让你的代码更具可读性,就像这个伪代码:

    if password's length is not okay:
        display 'at least 6, at max 10'
    else if passwords do not match:
        [...]
    else if password does not contain a letter: // if(!containsAtLeastOneLetter(passOne))
        display 'use at least one letter'
    [....]
    
于 2013-01-30T18:11:27.340 回答
0

显然,你的 for 循环中的问题,

如果你想检查字符串是否包含数字,你可以这样做:

Pattern.compile("[0-9]").matcher(passOne).find()

如果你想检查他的字符串是否包含字母:

Pattern.compile("[a-z]").matcher(passOne).find()

例如:

import java.util.regex.Pattern;

import javax.swing.JOptionPane;

public class Main {

    public static void main(String[] args) {
        String passOne, passTwo;
        passOne = JOptionPane.showInputDialog(null, "Please enter a password");
        passTwo = JOptionPane.showInputDialog(null, "Please re-enter your password");


        //Testing for a digit
        if (Pattern.compile("[0-9]").matcher(passOne).find()) {
            //Testing for a letter
            if (Pattern.compile("[a-z]").matcher(passOne).find()) {
                //Testing length 6-10 chars
                if (passOne.length() <= 10 && passOne.length() >= 6) {

                    //comparing two passwords
                    if (passOne.equals(passTwo)) {
                        JOptionPane.showMessageDialog(null, "Contratulations, you have a new password!");

                    } //If passwords don't match
                    else {
                         JOptionPane.showMessageDialog(null, "Passwords do not match, please try again.");

                    }
                } //If length is wrong
                else {
                    JOptionPane.showMessageDialog(null, "Password must be between 6 and 10 characters long.");
                }
            } //If no letter
            else {
                JOptionPane.showMessageDialog(null, "Password must contain at least one letter.");
            }

        } //If no digit
        else {
           JOptionPane.showMessageDialog(null, "Password must contain at least one digit.");

        }


    }
}
于 2013-01-30T18:40:39.307 回答
0

这是因为您为每个字符显示错误,使其无法通过验证。

解决方案的要点是,一旦发现错误,就必须跳出循环。然后,如果您确实有错误(您在爆发之前记录了该错误),则显示它。

我实际上并没有运行它,但它应该更接近,试试这个:

    String error = null;
    // Loop for each digit in password
    for (int x = 0; x < passOne.length(); x++) {
        // Testing for a digit
        if (Character.isDigit(passOne.charAt(x))) {
            // Testing for a letter
            if (Character.isLetter(passOne.charAt(x))) {
                // Testing length 6-10 chars
                if (passOne.length() <= 10 && passOne.length() >= 6) {
                    // comparing two passwords
                    if (passOne.equals(passTwo)) {
                        error = "Contratulations, you have a new password!";
                    }
                    // If passwords don't match
                    else {
                        error = "Passwords do not match, please try again.";
                    }
                }
                // If length is wrong
                else {
                    error = "Password must be between 6 and 10 characters long.";
                }
            }
            // If no letter
            else {

                error = "Password must contain at least one letter.";
            }

        }
        // If no digit
        else {
            error = "Password must contain at least one digit.";
        }

        if(error != null) break;

    }

    if(error != null) {
        JOptionPane.showMessageDialog(null, error);           
    }

}
于 2013-01-30T18:09:32.333 回答
0

您需要从 for 循环中取出等号检查。你只需要这样做一次,现在你正在做第一次的长度。

于 2013-01-30T18:09:46.940 回答