day08-常用类-String-练习3
This commit is contained in:
@@ -0,0 +1,47 @@
|
|||||||
|
package com.inmind.string01;
|
||||||
|
/*
|
||||||
|
校验密码是否合法。
|
||||||
|
- 必须至少8个字符。
|
||||||
|
- 必须至少2个大写字符。
|
||||||
|
- 必须只有字母和数字。
|
||||||
|
*/
|
||||||
|
public class Test09 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//定义一个密码
|
||||||
|
String password = "BAbbcc123";
|
||||||
|
boolean result = checkPassword(password);
|
||||||
|
System.out.println("该密码"+password+"是否合法:"+result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean checkPassword(String password) {
|
||||||
|
//- 必须至少8个字符。
|
||||||
|
if (password.length() < 8) {
|
||||||
|
System.out.println("密码长度不够8位");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//- 必须至少2个大写字符。
|
||||||
|
//- 必须只有字母和数字。
|
||||||
|
int count = 0;//大写字符的个数
|
||||||
|
char[] chars = password.toCharArray();
|
||||||
|
for (int i = 0; i < chars.length; i++) {
|
||||||
|
char c = chars[i];//获取当前字符
|
||||||
|
//先判断是否是大写字母
|
||||||
|
if (c >= 'A' && c <= 'Z') {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
//再判断是否是数字或者字母
|
||||||
|
if ((c < '0' || c > '9') && (c < 'A' || c > 'Z') && (c < 'a' || c > 'z')) {
|
||||||
|
System.out.println("密码中只能包含数字和字母");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//大写字符的数量
|
||||||
|
if (count < 2) {
|
||||||
|
System.out.println("密码中至少2个大写字母");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user