嘿我正在创建一个密码验证程序,要求程序检查数字,字母和密码的长度,然后程序会比较两个密码以查看它们是否匹配。一切正常,但是当显示错误消息时,它会显示多个消息框。我知道这与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.");
}
}
}
}
答案 0 :(得分:1)
您的代码有几个问题
您正在迭代密码的字符,但在每次迭代中,您都会检查整个密码的长度。你只需要在循环之外做一次。这也适用于检查passOne
和passTwo
“外包”代码以检查数字/字符,如:
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;
}
我故意发布了“仅包含数字和字母”代码,而不是“至少一位数”/“至少一个字母”btw;) 然后,您可以使您的代码更具可读性,例如Pseudocode:
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'
[....]
答案 1 :(得分: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);
}
}
答案 2 :(得分:0)
你需要从for循环中取出equals check。你只需要这样做一次,现在你正在进行第一次传递。
答案 3 :(得分: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.");
}
}
}