【发布时间】:2015-11-06 03:15:38
【问题描述】:
好的,所以我正在制作一个密码检查程序,并且有一些规则。 密码必须为 8 个或更多字符 密码必须包含 2 位或更多位 密码只能是字母和数字 这是我到目前为止所拥有的 如何检查 2 位数字以及仅检查字母和数字?谢谢
import java.util.Scanner;
public class checkPassword {
public static boolean passwordLength(String password) {
boolean correct = true;
int digit = 0;
if (password.length() < 8) {
correct = false;
}
return correct;
}
public static void main(String[] args) {
// Nikki Kulyk
//Declare the variables
String password;
Scanner input = new Scanner(System.in);
//Welcome the user
System.out.println("Welcome to the password checker!");
//Ask the user for input
System.out.println("Here are some rules for the password because we like to be complicated:\n");
System.out.println("A password must contain at least eight characters.\n" +
"A password consists of only letters and digits.\n" +
"A password must contain at least two digits.\n");
System.out.println("Enter the password: ");
password = input.nextLine();
boolean correct = passwordLength(password);
if (correct) {
System.out.println("Your password is valid.");
}
else {
System.out.println("Your password is invalid.");
}
}
}
【问题讨论】:
-
检查这个link你会发现一些验证方法。
-
谢谢,但我正在寻找一个实际的解释,而不仅仅是一个代码,因为我是一个初学者程序员,我并不完全理解所有这些东西的含义。
标签: java