似乎只有当我在数字数组中重复第一个值(0)5次时才能工作。我正在处理规则3,其中:不得包含5个或更多的字母或数字(例如,'abcde12'和'12345ab','a12345b'和'1abced2'无效,但'abc123de45'有效)。
import java.util.Scanner;
public class Assignment5E3{
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.println("Please enter a new password: ");
String password = reader.next();
String firstRule = ruleOne(password);
String secondRule = ruleTwo(password);
String thirdRule = ruleThree(password);
}
public static String ruleOne(String password){
if( password.length() < 5 || password.length() > 12 ){
System.out.println("Password must be between 5 and 12 characters!");
}else{
System.out.println(password);
}
return password;
}
public static String ruleTwo(String password){
int j = 0;
int k = 0;
for(int i=0; i<password.length(); i++){
char ch = password.charAt(i);
if(Character.isLetterOrDigit(ch)){
j++;
}else if(!Character.isLetterOrDigit(ch)){
k++;
}
}
if(k > 0){
System.out.println("Password may only contain letters and numbers");
}
return password;
}
public static String ruleThree(String password){
char[] input = password.toCharArray();
char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
char[] numbers = "0123456789".toCharArray();
for(int i = 0; i<input.length-1; i++){
for(int j = 0; j<numbers.length-1; j++){
if((input[i] == numbers[j+1]) && (input[i+1] == numbers[j+2]) && (input[i+2] == numbers[j+3]) && (input[i+3] == numbers[j+4]) && (input[i+4] == numbers[j+5])){
System.out.println("Password cannot have 5 numbers in sequence!");
}
}
for(int k = 0; k<letters.length-1; k++){
if((input[i] == letters[k+1]) && (input[i+1] == letters[k+2]) && (input[i+2] == letters[k+3]) && (input[i+3] == letters[k+4]) && (input[i+4] == letters[k+5])){
System.out.println("Password cannot have 5 letters in sequence!");
}
}
}
return password;
}
}
这是解决方案:
import java.util.Scanner;
public class Assignment5E3{
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
// receiving input from user to check for password validity
System.out.println("Please enter a new password: ");
String password = reader.next();
// calling first method to check if the password is between 5 and 12 characters
String firstRule = ruleOne(password);
// calling second method to check if the password only contains letters and digits
String secondRule = ruleTwo(password);
// calling third method to make sure that there is not 5 letters or digits in sequence within the password
String thirdRule = ruleThree(password);
}
// method of rule one, this will check to see if the password is between 5 and 12 characters long
public static String ruleOne(String password){
if( password.length() < 5 || password.length() > 12 ){
System.out.println("Password must be between 5 and 12 characters!");
}else{
System.out.println(password);
}
return password;
}
// method of rule two, this will check to see if the password will only contain letters and digits
public static String ruleTwo(String password){
int j = 0;
int k = 0;
for(int i=0; i<password.length(); i++){
char ch = password.charAt(i);
if(Character.isLetterOrDigit(ch)){
j++;
}else if(!Character.isLetterOrDigit(ch)){
k++;
}
}
if(k > 0){
System.out.println("Password may only contain letters and numbers");
}
return password;
}
// method of rule three, this will check to see if the password does not contain 5 letters or digits in sequence
// within the password
public static String ruleThree(String password){
char[] input = password.toCharArray();
char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
char[] numbers = "0123456789".toCharArray();
// since password length is already greater than 5 chars, I need to -5 in loop so that the index is not out of bounds
for(int i = 0; i<input.length-5; i++){
for(int j = 0; j<numbers.length-1; j++){
if((input[i] == numbers[j+1]) && (input[i+1] == numbers[j+2]) && (input[i+2] == numbers[j+3]) && (input[i+3] == numbers[j+4]) && (input[i+4] == numbers[j+5])){
System.out.println("Password cannot have 5 numbers in sequence!");
}
}
for(int k = 0; k<letters.length-1; k++){
if((input[i] == letters[k+1]) && (input[i+1] == letters[k+2]) && (input[i+2] == letters[k+3]) && (input[i+3] == letters[k+4]) && (input[i+4] == letters[k+5])){
System.out.println("Password cannot have 5 letters in sequence!");
}
}
}
return password;
}
}
发布于 2018-08-02 12:34:50
你想要的东西:
boolean hasLessThan5CharsInSequence(String password)
你希望能够调用此类方法,以便知道你的输入是否符合规则。向用户打印消息不是此方法应该担心的问题。
为了进行检查,我建议你迭代传入的字符串两次。一次检查信件,一次检查数字。
https://stackoverflow.com/questions/-100001861
复制相似问题