嘿,我正在创建一个密码验证程序,该程序要求程序检查密码的数字、字母和长度,然后程序比较两个密码以查看它们是否匹配。一切正常,但是当显示错误消息时,它会显示多个消息框。我知道这与 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.");
}
}
}
}