从键盘录入一个字符串作为密码,打印密码强度。
"Abc@123"
int n0 = 0; // 大写字母个数
int n1 = 0; // 小写字母个数
int n2 = 0; // 数字个数
int n3 = 0; // 其他字符的个数
判断密码强度:
前提:长度大于8;
只有一类字符,则密码弱;
有两类字符,密码一般;
有三类字符,密码强;
有四类字符,密码很强
import java.util.Scanner;
public class PasswordStrength {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入密码:");
String password = scanner.nextLine();
int n0 = 0; // 大写字母个数
int n1 = 0; // 小写字母个数
int n2 = 0; // 数字个数
int n3 = 0; // 其他字符的个数
// 统计密码中各种字符的个数
for (int i = 0; i < password.length(); i++) {
char c = password.charAt(i);
if (Character.isUpperCase(c)) {
n0++;
} else if (Character.isLowerCase(c)) {
n1++;
} else if (Character.isDigit(c)) {
n2++;
} else {
n3++;
}
}
// 判断密码强度
int count = 0;
if (n0 > 0) {
count++;
}
if (n1 > 0) {
count++;
}
if (n2 > 0) {
count++;
}
if (n3 > 0) {
count++;
}
if (count == 1) {
System.out.println("密码弱");
} else if (count == 2) {
System.out.println("密码一般");
} else if (count == 3) {
System.out.println("密码强");
} else {
System.out.println("密码很强");
}
}
}
代码实现的思路是先统计密码中各种字符的个数,然后根据不同种类字符的个数判断密码的强度,最后输出密码的强度。
我发现网络上很多介绍多线程的案例感觉都是错误的例子。也不能说错的,例如用异步并发队列的时候,他们只是打印了一个log,这种打印的行为本身就是同步任务,肯定按照最简单的例子进行打印了,看到的效果自然是很多文章所说的。但是如果你Block里面的任务是网络请求呢?还能保证网络异步任务的一致性?很显然大部分文章压根没有考虑任务的异步性,而且显示开发中大部分任务都是异步的,因此本文先从网上的同步任务,也就是比较简单的案例开始介绍,最后再通过一道阿里的面试题来引出实际开发中的问题,这个题目非常有代表性。