7-1 检查密码 (15 分)
(Java版)7-1 检查密码 (15 分)
本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能。该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母、数字和小数点 .,还必须既有字母也有数字。
(代码中的不足还请大家指正)
输入格式:
输入第一行给出一个正整数 N(≤ 100),随后 N 行,每行给出一个用户设置的密码,为不超过 80 个字符的非空字符串,以回车结束。
注意: 题目保证不存在只有小数点的输入。
输出格式:
对每个用户的密码,在一行中输出系统反馈信息,分以下5种:
如果密码合法,输出Your password is wan mei.;
如果密码太短,不论合法与否,都输出Your password is tai duan le.;
如果密码长度合法,但存在不合法字符,则输出Your password is tai luan le.;
如果密码长度合法,但只有字母没有数字,则输出Your password needs shu zi.;
如果密码长度合法,但只有数字没有字母,则输出Your password needs zi mu.。
输入样例:
5
123s
zheshi.wodepw
1234.5678
WanMei23333
pass*word.6
输出样例:
Your password is tai duan le.
Your password needs shu zi.
Your password needs zi mu.
Your password is wan mei.
Your password is tai luan le.
非标准个人代码
import java.util.ArrayList;
import java.util.Scanner;
public class PtaSYJ {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
sc.nextLine();
ArrayList<String> arr = new ArrayList<>();
for (int i = 0; i < count; i++) {
String str = sc.nextLine();//将输入的密码一字符串接收
arr.add(str);//将密码存入动态数组中
}
for (int i = 0; i < arr.size(); i++) {//几个密码循环几次
int a = 0;
int b = 0;
int d = 0;
int c=0;
String str = arr.get(i);//str是密码字符串
if (str.length() < 6) //检测密码长度
System.out.println("Your password is tai duan le.");
else
for (int j = 0; j < str.length(); j++)
{//第一个密码的第一个字符
char one = str.charAt(j);//获取第j个字符
String two=String.valueOf(str.charAt(j));//将j转换成String类型
String the = ".";
if (Character.isDigit(str.charAt(j))) //检测数字
a++;
else if (Character.isLetter(str.charAt(j))) //检测字母
b++;
else if (two.equals(the))
c++;//没有"."时c+ 有.时c=0
else
d++;//检测不是数字、字母、’。‘的字符
}
//第一个for循环结束
if (d > 0) //密码中有不合法字符
System.out.println("Your password is tai luan le.");
else if (a > 0 && b > 0) //既有数字也有字母
System.out.println("Your password is wan mei.");
else if (a > 0) //只有数字
System.out.println("Your password needs zi mu.");
else if (b > 0) //只有字母
System.out.println("Your password needs shu zi.");
}
}
}