利用Java提供的异常处理机制实现以下功能:
检查用户输入的密码是否满足:
一、长度大于6
二、必须包含数字、大写字母和小写字母
//package text2;
//import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public int flag;
public void regist(String s) throws Exception {
int len = s.length();
int i, f1 = 0, f2 = 0, f3 = 0, f4 = 0;
if (len > 6) {
f4 = 1;
}
for (i = 0; i < s.length(); i++) {
if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
f1 = 1;
} else if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
f2 = 1;
} else if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
f3 = 1;
}
}
if (f4 == 0)
throw new length("密码长度需要大于6!");
else if (f1 == 0 || f2 == 0 || f3 == 0)
throw new secretwords("你的密码长度符合要求,但你的密码必须包含数字,小写和大写字母这三部分!");
System.out.println("密码符合正确规定");
}
class length extends Exception {
private static final long serialVersionUID = 1L;
public length(String s) {
super(s);
}
}
class secretwords extends Exception {
private static final long serialVersionUID = 1L;
public secretwords(String s) {
super(s);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
try {
Main a = new Main();
a.regist(s);
} catch (Exception e) {
if (e instanceof length) {
e.printStackTrace();
}
if (e instanceof secretwords) {
e.printStackTrace();
}
}
sc.close();
}
}