0

我想编写一个简单的 java 程序来检查 keycode 是否匹配一组条件。

这是我到目前为止所拥有的:

Scanner keycode = new Scanner(System.in);
System.out.println("Input keycode");
String key1 = keycode.nextLine();

do {

    if (key1.length() < 6 || key1.length() > 8) {

        System.out.println("must be at least 6 letter and max 8 letter");
        return;
    }

    else {

        boolean upper = false;
        boolean lower = false;
        boolean number = false;

        for (char c : key1.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");
            return;
        } else if (!lower) {
            System.out.println("must contain at least one lowercase character");
            return;
        } else if (!number) {
            System.out.println("must contain at least one number");
            return;
        } else {
            return;
        }
    }

} while (true);

System.out.println("Input keycode again");
String key2 = keycode.nextLine();

if (key1.equals(key2)) {

    System.out.println("keycode matched");
} else {
    System.out.println("keycode dont match");

}

提示用户输入键码。

程序首先检查是否超过 6 个字符和少于 8 个字符。

然后它检查它是否包含小写、大写和数字。

我希望它允许用户在犯任何错误时再次输入密码,而不是重新输入密码。

如果成功,它将要求用户再次输入键码。如果两个键码不匹配,则允许用户重试。3 次尝试失败后,系统将回复“keycode mismatch”。

我需要帮助的部分是如果密码不符合要求并且密码不匹配,则允许用户输入密码。当我输入少于 6 个字符的密码时,我得到以下输出:

must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
4

4 回答 4

2

使用循环

boolean flag=false;
while(flag!=true)
{
    Scanner keycode =  new Scanner(System.in);
    System.out.println("Input keycode");
    String key1 = keycode.nextLine();
    if (key1.length() < 6 || key1.length() > 8) {

    System.out.println("must be at least 6 letter and max 8 letter");

    } 
    else
    {
         flag=true;
    }
}
于 2012-10-24T08:40:59.117 回答
0

您应该将该行String key1 = keycode.nextLine();放在 do 循环中,以便每次需要时都向用户查询一次。否则,您只是陷入了一个循环,该循环总是会一次又一次地检查相同的值。

于 2012-10-24T08:41:12.587 回答
0

使用boolean变量 say redo。并且在您使用 return 的每个地方,将其替换为redo = true. 并且您的 while 条件应该是while(redo),它将一直运行,直到redo is true用户没有输入正确的数字。

另外,移动你的input阅读线: -

String key1 = keycode.nextLine();

在你的 do-while 循环中。而且,将你的redo变量重置false为循环中,这样它就不会true永远存在。

String[] keys = new String[2];  // Since you are reading 2 keys, so size = 2
boolean redo = false;
int count = 0;    // To maintain the number of keys read. should stop at count = 2

do {

    redo = false;
    Scanner keycode =  new Scanner(System.in);

    System.out.println("Input keycode No: + " + (count + 1));
    String key1 = keycode.nextLine();

    if (key1.length() < 6 || key1.length() > 8) {
        redo = true;

    } else {
        /** Your remaining code **/

        if (!upper) {
            System.out.println("must contain at least one uppercase character");
            redo = true;

        } else if (!lower) {
            System.out.println("must contain at least one lowercase character");
            redo = true;

        } else if (!number) {
            System.out.println("must contain at least one number");
            redo = true;

        } else {
            keys[count++] = key1;  // Enter a new key in array
        }
    }

} while (redo || count < 2);  

if (keys[0].equals(keys[1])) {
    System.out.println("Keys are equal");
}
于 2012-10-24T08:44:01.547 回答
0
boolean redo = false;

do {
    redo = false;
    Scanner keycode =  new Scanner(System.in);
    System.out.println("Input keycode");
    String key1 = keycode.nextLine();

    if (key1.length() < 6 || key1.length() > 8) {
        redo = true;

    } else {
        /** Your remaining code **/

        if (!upper) {
            System.out.println("must contain at least one uppercase character");
            redo = true;

        } else if (!lower) {
            System.out.println("must contain at least one lowercase character");
            redo = true;

        } else if (!number) {
            System.out.println("must contain at least one number");
            redo = true;
        }

          else{
          System.out.println("Input keycode again");
          String key2 = keycode.nextLine();

          if (key1.equals(key2)) {

           System.out.println("keycode matched");
            } else {
             System.out.println("keycode dont match");

             }

       }

    }

} while (redo);

我设法将代码放入此处,不确定其是否正确?

于 2012-10-24T09:45:14.220 回答