gpt4 book ai didi

java - 帮助在 java 中制作密码检查器

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:33 25 4
gpt4 key购买 nike

我正在尝试用 Java 编写一个程序来检查三个特定的输入。它必须通过这些测试:

  1. 至少 7 个字符。
  2. 同时包含大写小写字母字符。
  3. 至少包含 1 个数字。

到目前为止,我已经能够检查是否有 7 个字符,但我在处理最后两个字符时遇到了问题。我应该在我的循环中放入什么作为 if 语句来检查数字并将其设为大写和小写。任何帮助将不胜感激。这是我目前所拥有的。

import java.awt.*;
import java.io.*;
import java.util.StringTokenizer;

public class passCheck
{


private static String getStrSys ()
{
String myInput = null; //Store the String that is read in from the command line

BufferedReader mySystem; //Buffer to store the input

mySystem = new BufferedReader (new InputStreamReader (System.in)); //creates a connection to system input
try
{
myInput = mySystem.readLine (); //reads in data from the console
myInput = myInput.trim ();
}
catch (IOException e) //check
{
System.out.println ("IOException: " + e);
return "";
}
return myInput; //return the integer to the main program
}


//****************************************
//main instructions go here
//****************************************

static public void main (String[] args)
{

String pass; //the words the user inputs
String temp = ""; //holds temp info

int stringLength; //length of string
boolean goodPass = false;


System.out.print ("Please enter a password: "); //ask for words

pass = getStrSys (); //get words from system
temp = pass.toLowerCase ();
stringLength = pass.length (); //find length of eveyrthing



while (goodPass == false)

{
if (stringLength < 7)
{
System.out.println ("Your password must consist of at least 7 characters");
System.out.print ("Please enter a password: "); //ask for words
pass = getStrSys ();
stringLength = pass.length ();
goodPass = false;
}
else if (/* something to check for digits */)
{

}

}

最佳答案

当然你可以想出一个复杂的——几乎不可读——正则表达式来做这个,但我不建议这样做。除了可读性方面,如果密码失败,它不会告诉您原因。这解决了这两个问题:

while (true) {
pass = getStrSys();
if (pass.length() < 7) {
System.out.println("must be at least 7 characters long");
} else {
boolean upper = false;
boolean lower = false;
boolean number = false;
for (char c : pass.toCharArray()) {
if (Character.isUpperCase(c)) {
upper = true;
} else if (Character.isLowerCase(c)) {
lower = true;
} else if (Character.isDigit(c)) {
number = true;
}
}
if (!upper) {
System.out.println("must contain at least one uppercase character");
} else if (!lower) {
System.out.println("must contain at least one lowercase character");
} else if (!number) {
System.out.println("must contain at least one number");
} else {
break;
}
}
}

关于java - 帮助在 java 中制作密码检查器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2570043/

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