From 5202e64a574d5126ad02888c7f0b21ec81be1779 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Wed, 22 Jul 2026 14:52:03 +0800 Subject: [PATCH] =?UTF-8?q?day08-=E5=B8=B8=E7=94=A8=E7=B1=BB-String-?= =?UTF-8?q?=E7=BB=83=E4=B9=A03?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day08/src/com/inmind/string01/Test09.java | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 day08/src/com/inmind/string01/Test09.java diff --git a/day08/src/com/inmind/string01/Test09.java b/day08/src/com/inmind/string01/Test09.java new file mode 100644 index 0000000..030aece --- /dev/null +++ b/day08/src/com/inmind/string01/Test09.java @@ -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; + } +}