一些网站对于密码具有一些规则。编写一个方法,检测字符串是否是一个有效密码。
1.密码必须至少8位字符。
2.密码仅能包含字母和数字。
3.密码必须包含两个数字。
package hello;//JAVA程序在SRC里无需此句
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
String password = input.next();
password(password);
}
public static void password (String password) {
int biaozhi = 1;
if(password.length() >= 8){
password = password.toUpperCase();
for(int i = 0, count = 0; i < password.length(); i++) {
if((password.charAt(i) >= '0' && password.charAt(i) <='9')
||(password.charAt(i) >= 'A' && password.charAt(i)
< 'Z')){
if(password.charAt(i) >= '0' && password.charAt(i) <='9') {
++count;
if(count >= 2)
System.out.println("ValidPassword");
else
System.out.println("InValidPassword");
}
else
System.out.println("InvalidPassword");
System.exit(0);
}
}
}
else
System.out.println("InValidPassword");
}
}