【发布时间】:2023-04-06 06:42:01
【问题描述】:
我是一名 CIS 的学生,正在学习 Java 课程(希望成为一名程序员)。无论如何,我有一个任务是创建一个检查密码有效性的程序。如果密码无效,则打印一条消息。如果密码有效,用户将重新插入密码。如果它们都匹配,则打印一条消息接受。如果它们不匹配,则打印一条消息。 密码要求是:
- 长度必须至少为 8 个字符
- 必须包含一个数字
- 必须包含一个字母
- 必须包含一个特殊字符
- 不能有 3 个或更多相同的字符
- 不能包含空格
- 不能以 !还是?
我正在使用 netbeans。
package pass1;
import java.util.Scanner;
public class Pass1 {
//Main method asks for pasword from user and calls upon methods to verify password requirements and password 1 and 2 match
public static void main(String[] args) {
//declare variables
String pass1;
String pass2;
boolean passvalid;
boolean passmatch;
//initialize new instance of scanner
Scanner kb = new Scanner(System.in);
//get password
System.out.println("Please input password. (Must be at least 8 characters, contain a number, letter, and special character");
pass1 = kb.nextLine();
//initialize instance of passisvalid to check password for requirements
passvalid = passisvalid(pass1);
//if pass is valid, allow user to reinsert password
if (passvalid){
System.out.println("Please reinsert password:");
pass2 = kb.nextLine();
//initialize instance of passismatch to check passwords match
passmatch = passismatch(pass1, pass2);
//if passwords do not match, print message
if (!passmatch)
{
System.out.println("Passwords do not match.");
}
//if passwords match, print message
else if (passmatch)
{
System.out.println("Password set.");
}
}
//if password is not valid, print message
else
{
System.out.println("Password is not valid:");
}
}
/*************************************************************************************/
//this method check that user inputted password meets requirements, and returns boolean value
public static boolean passisvalid(String password) {
//declare variables
boolean letter;
boolean digit;
boolean space;
boolean length;
boolean start1;
boolean start2;
boolean valid;
boolean special;
//initialize variables
valid=false;
letter=false;
digit=false;
space=false;
special=false;
length = false;
start1=false;
start2=false;
//initialize count
for(int i=0;i<password.length();i++)
{
char s=password.charAt(i);
//check for letter in password
if(Character.isLetter(s))
{
letter = true;
}
//check for number in password
if(Character.isDigit(s))
{
digit = true;
}
//check for spaces in password
if(Character.isSpaceChar(s))
{
space=true;
}
//check for special characters in password
if(!Character.isDigit(s) && !Character.isLetter(s))
{
special=true;
}
}
//check password length
if (password.length() > 8)
{
length=true;
}
//check password start with ? or !
else if (password.startsWith("?"))
{
start1=true;
}
else if (password.startsWith("!"))
{
start1=true;
}
//requirements of password for boolean true
if(letter && digit && special && length)
{
valid = true;
}
//return boolean false if detect start with ? or !, or spaces in password
if(start1||start2||space)
{
valid = false;
}
return valid;
}
/**********************************************************************************/
//this method checks that both user entered passwords match
public static boolean passismatch(String password1, String password2){
//declare variables
boolean match;
//initialize variables
match=false;
//compare password strings
if (password1.equals(password2))
{
match= true;
}
return match;
}
}
所以除了确保没有连续的重复字符之外,我已经能够计算出所有内容。我真的不知道从哪里开始。我在几个论坛上搜索了几个小时,试图找到一个解决方案并将它们放在一起,但没有看到一个例子,或者只是一些我一生都无法弄清楚的东西。任何帮助将不胜感激。
【问题讨论】:
-
嗯,这听起来像是一个递归函数
-
布尔 noConsecutive = true;从[0...n-2]循环i,比较password[i]和password[i+1],如果相同,noConsecutive = false;
-
我不完全明白如何做到这一点,你能更深入地解释一下吗?我对循环真的很糟糕:-\。 IE。我会在哪里告诉循环转到负 2?我会用什么来比较字符?
-
你可以看看这个问题在 SO.stackoverflow.com/questions/19605150/…
-
是的,我见过那个。他们不要求没有连续的相同字符
标签: java validation passwords character