for(int i=0;i
sum1++;break;
}
这里,break应该在if里,没有写{},还有,你写的只是读取一行
public class MakePwd {
public static void main(String[] args) {
demo2();
}
public static void demo1(){
System.out.println("请输入行数:");
Scanner line = new Scanner(System.in);
int lineNum = line.nextInt();
for(int i = 0; i < lineNum; i++){
System.out.println("请输入第" + (i+1) + "行:");
System.out.println();
Scanner xx = new Scanner(System.in);
String input = xx.nextLine();
if(input == null || input.length() == 0){
System.out.println("没有输入,作废,程序结束");
return;
}else if(input.length() < 8 || input.length() > 16){
System.out.println("密码长度大于等于8,且不要超过16");
System.out.println();
continue;
}
if(!check(input)){
System.out.println("本行错误");
System.out.println();
}
System.out.println("输入完成");
}
}
public static void demo2(){
System.out.println("请输入行数:");
Scanner line = new Scanner(System.in);
int lineNum = line.nextInt();
Listlist = new ArrayList ();
for(int i = 0; i < lineNum; i++){
Scanner xx = new Scanner(System.in);
String input = xx.nextLine();
list.add(input);
if(input == null || input.length() == 0){
System.out.println("没有输入,作废,程序结束");
return;
}else if(input.length() < 8 || input.length() > 16){
System.out.println("密码长度大于等于8,且不要超过16,程序结束");
System.out.println();
return;
}
}
System.out.println("输入完成");
for(String str : list){
check(str);
}
}
public static boolean check(String c){
int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
for (int i = 0; i < c.length(); i++) {
if (c.charAt(i) >= 'a' && c.charAt(i) <= 'z'){
sum1++;
break;
}
}
for (int i = 0; i < c.length(); i++) {
if (c.charAt(i) >= 'A' && c.charAt(i) <= 'Z') {
sum2++;
break;
}
}
for (int i = 0; i < c.length(); i++) {
if (c.charAt(i) >= '0' && c.charAt(i) <= '9') {
sum3++;
break;
}
}
for (int i = 0; i < c.length(); i++) {
if (c.charAt(i) == '~' || c.charAt(i) == '!'
|| c.charAt(i) == '@' || c.charAt(i) == '#'
|| c.charAt(i) == '$' || c.charAt(i) == '%'
|| c.charAt(i) == '^') {
sum4++;
break;
}
}
if ((sum1 + sum2 + sum3 + sum4) >= 3 && c.length() >= 8
&& c.length() <= 16) {
System.out.println("YES");
return true;
} else {
System.out.println("NO");
return false;
}
}
}
简单写了个例子,编程时注意规范