【发布时间】:2014-10-15 23:17:59
【问题描述】:
我的问题是创建一个包含 6 到 10 个字符且至少包含一个字母和一个数字的密码,然后让用户重新输入密码并确认它们匹配。
我唯一遇到的问题是检查密码是否包含字母或数字。我在网站上浏览并发现了同样的问题,但我对他们在代码中引用的一些方法和其他东西感到困惑,因为我似乎以不同的方式构建我的。我想过使用 indexOf() 方法来查看它是否返回 -1 值,但我不确定从哪里开始。
我真的是 java 新手,我确信有一种更有效的方法来构建它,我希望有任何提示。
import java.util.Scanner;
public class Password
{
public static void main(String[] args)
{
//Input from user
String password;
Scanner input = new Scanner(System.in);
System.out.print("Please create your password: ");
password = input.nextLine();
//Checking password length
while( (password.length() < 6) || (password.length() > 10) )
{
System.out.print("This password must be between 6 and 10 characters. Try again: ");
password = input.nextLine();
}
//Checking to see if passwords contain digit/letter
/*Need to add code here */
//Confirming if passwords match
String password2;
System.out.print("\nPlease type your password again to confirm: ");
password2 = input.nextLine();
while( !password2.equals(password) )
{
System.out.print("Those passwords do not match. Try again: ");
password2 = input.nextLine();
}
}
}
【问题讨论】:
-
您可能想了解正则表达式。