gpt4 book ai didi

java - 如何检查密码是否有数字?

转载 作者:行者123 更新时间:2023-12-01 13:41:36 25 4
gpt4 key购买 nike

如何检查密码是否有数字?我已经办理了支票对于长度,但密码也需要数字,我该如何实现?我知道它与 char 类和循环有关,但我尝试过的一切都让我陷入了困境。任何建议都会有帮助。

import javax.swing.JOptionPane;
public class Pasword
{
public static void main(String[] args)
{
String password1 = JOptionPane.showInputDialog(null, "Please Enter A Password That Contains 6 - 10 Characters One Of Which Is A Number");
int len = password1.length();

while((len < 6) || (len > 10))
{
password1 = JOptionPane.showInputDialog(null, "! INVALID !Please Enter A Password That Contains 6 - 10 Characters One Of Which Is A Number");
len = password1.length();
}

String password2 = JOptionPane.showInputDialog(null, "!PASSWORD VALIDATOR! Please Re-Enter Password");
int len2 = password2.length();

while((len2 < 6) || (len2 > 10))
{
password2 = JOptionPane.showInputDialog(null, "! INVALID !Please Re-Enter");
len2 = password2.length();
}
if(password1.equals(password2))
{
JOptionPane.showMessageDialog(null, "Accepted");
}
else
{
JOptionPane.showInputDialog(null, "Passwords do not match, Pleas Re-enter");
}

}

}

我是java新手,已经学到了很多关于它的知识,我只是在实现这些知识时遇到了问题。谢谢您

最佳答案

我会使用一些正则表达式。

System.out.println("x123".matches("(.*?)\\d(.*?)"));
System.out.println("xyz".matches("(.*?)\\d(.*?)"));

关于java - 如何检查密码是否有数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20713289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com