gpt4 book ai didi

java - 提示用户输入与特定模式匹配的密码

转载 作者:行者123 更新时间:2023-12-02 12:50:58 25 4
gpt4 key购买 nike

我已经编写了一些代码,但我似乎无法弄清楚如何获取字符、数字或符号的确切数量。我稍微修正了我的代码,但它不起作用,我不知道为什么。

我的要求是

Write a Java program that prompts the user to enter a password that matches a specific pattern. Your program must approve the user's entry.. Here is the pattern, in this order:

-1 or more upper case letters
-two lower case letters
-1 or 2 digits
-zero or 1 upper case letters
-any two of this group @#$%^&

我的代码:

import java.util.Scanner;
public class TestingCenter {

public static void main(String[] args) {
int digit=0;
int special=0;
int upCount=0;
int upCount2=0;
int loCount=0;
String password;
Scanner scan = new Scanner(System.in);
System.out.println(" Enter Your Password:");
password = scan.nextLine();

for(int i =0;i<password.length();i++){
char c = password.charAt(i);
if(Character.isUpperCase(c)){
upCount++;
}
if(Character.isLowerCase(c)){
loCount++;
}
if(Character.isDigit(c)){
digit++;
}
if(Character.isUpperCase(c)){
upCount2++;
}
if(c>=33&&c<=46||c==64){
special++;
}
}
if(special==2&&loCount==2&&upCount>=1&&(digit==1||digit==2)&&upCount2<=1){
System.out.println(" Password is good:");
}
}
}

最佳答案

如果我理解正确的话,按此顺序字面意思是按给定的顺序。

如果是这种情况,您需要正则表达式。忘记计算字符数。

  • 1 个或多个大写字母[A-Z]+
  • 两个小写字母[a-z]{2}
  • 1 或 2 位数字\d{1,2}
  • 0 个或 1 个大写字母[A-Z]?
  • 该组中的任意两个 @#$%^& [@#$%^&]{2}

所以,

Scanner scan = new Scanner(System.in);
System.out.println(" Enter Your Password:");
String password = scan.nextLine();
System.out.println(password.matches("[A-Z]+[a-z]{2}\\d{1,2}[A-Z]?[@#$%^&]{2}");

如果这不是您的指令的意思,请检查您的条件。显然并非所有内容都应该是 >= 1

关于java - 提示用户输入与特定模式匹配的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44611841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com