我必须编写密码验证程序。
您将拥有以下方法:
main(String []):调用getValidPassword
getValidPassword():检查密码是否具有所有条件并打印确认语句(如果有效),否则输出无效语句并接受新密码
checkLength(String):如果密码String符合长度条件,则返回true,否则返回false
checkUpperCase(String):如果密码String有足够的大写字母,则返回true,否则返回false
checkLowerCase(String):如果密码String具有足够的小写字母,则返回true,否则返回false
checkNumbers(String):如果密码String有足够的数字,则返回true,否则返回false
以上列表的格式为:
methodName(listOfParameterTypes):功能和返回值(如果有) 请务必严格按照本实验中的说明保留方法名称。
在这里输入代码
import java.util.Scanner;
public class PasswordValidator {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String password=sc.nextLine();
getValidPassword(password);
}
public static void getValidPassword(String password) {
boolean valid;
if ( checkLength(password) && checkUpperCase(password) && checkLowerCase
(password) && checkNumbers(password) ) {
System.out.print("Password accepted");
valid=true;
}
else {
System.out.print("Invalid password, try again");
valid=false;
}
}
//checks PASSWORDS length
public static boolean checkLength(String password) {
boolean length;
if (password.length() >= 8 ) {
length=true;
}
else {
length=false;
}
return length;
}//finish length check
//checks uppercase
public static boolean checkUpperCase(String password) {
int j=0;
boolean upper;
//count uppercases
String word=password.toUpperCase();
for (int i=0; i<password.length(); ++i) {
if (password.charAt(i)==word.charAt(i)) {
++j;
}
else {
j=j;
}
}
if (j>=2) {
upper=true;
}
else {
upper=false;
}
return upper;
}//finish uppercase check
//checks lowercase
public static boolean checkLowerCase(String password) {
int j=0;
String word=password.toLowerCase();
boolean low;
//count lower cases
for (int i=0; i<password.length(); ++i) {
if (password.charAt(i)==word.charAt(i)) {
++j;
}
else {
j=j;
}
}
if (j>=1) {
low=true;
}
else {
low=false;
}
return low;
}//finish lowercase
//check numbers
public static boolean checkNumbers(String password) {
int j=0;
boolean num;
for (int i=0; i<password.length(); ++i) {
if (Character.isDigit(password.charAt(i))) {
++j;
}
else {
j=j;
}
}
if (j>=2) {
num=true;
}
else {
num=false;
}
return num;
}//finish numbers
}
答案 0 :(得分:0)
import java.util.Scanner;
public class PasswordValidator {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String password=sc.nextLine();
while(getValidPassword(password))
{
System.out.println("Wrong Password!! Try Again");
}
}
public static boolean getValidPassword(String password) {
boolean valid;
if ( checkLength(password) && checkUpperCase(password) && checkLowerCase(password) && checkNumbers(password) ) {
System.out.print("Password accepted");
valid=true;
}
else {
System.out.print("Invalid password, try again");
valid=false;
}
return valid;
}
//checks PASSWORDS length
public static boolean checkLength(String password) {
boolean length;
if (password.length() >= 8 ) {
length=true;
}
else {
length=false;
}
return length;
}//finish length check
//checks uppercase
public static boolean checkUpperCase(String password) {
int j=0;
boolean upper;
//count uppercases
String word=password.toUpperCase();
for (int i=0; i<password.length(); ++i) {
if (password.charAt(i)==word.charAt(i)) {
++j;
}
else {
j=j;
}
}
if (j>=2) {
upper=true;
}
else {
upper=false;
}
return upper;
}//finish uppercase check
//checks lowercase
public static boolean checkLowerCase(String password) {
int j=0;
String word=password.toLowerCase();
boolean low;
//count lower cases
for (int i=0; i<password.length(); ++i) {
if (password.charAt(i)==word.charAt(i)) {
++j;
}
else {
j=j;
}
}
if (j>=1) {
low=true;
}
else {
low=false;
}
return low;
}//finish lowercase
//check numbers
public static boolean checkNumbers(String password) {
int j=0;
boolean num;
for (int i=0; i<password.length(); ++i) {
if (Character.isDigit(password.charAt(i))) {
++j;
}
else {
j=j;
}
}
if (j>=2) {
num=true;
}
else {
num=false;
}
return num;
}//finish numbers
}
答案 1 :(得分:0)
分离所有IO并专注于该方法。
public class PasswordValidator {
public static boolean isValidPassword(String password) {
boolean isValid = false;
// add logic and rules here
return isValid;
}
}
使用JUnit测试它,而不是命令行输入。
public class PasswordValidatorTest {
@Test
public void testIsValidPassword_Success() {
// setup
// exercise
// assert
}
@Test
public void testIsValidPassword_Failure() {
// setup
// exercise
// assert
}
}