【问题标题】:method to check if password/keycode match检查密码/密钥代码是否匹配的方法
【发布时间】:2012-10-24 08:34:42
【问题描述】:

我想写一个简单的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

【问题讨论】:

  • 请格式化您的代码而不发布。您的最后一个代码也格式不正确。在这种状态下很难阅读您的代码。
  • 再次,使用布尔变量redo。在您使用过return 的每个地方,将其替换为redo = true。而你的while条件应该是while(redo);
  • 显示错误信息后必须再次扫描密码
  • 我的 Eclipse IDe 告诉我,在 do-while 循环之后包含“无法访问的代码”

标签: java passwords keycode


【解决方案1】:

使用循环

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;
    }
}

【讨论】:

  • while 循环在这种情况下比 do while 更好吗?
  • 不应将while(flag!=true) 替换为while(!flag)
  • @newbieprogrammer。 do-while 更好,因为您至少想运行一次。
【解决方案2】:

使用boolean 变量,例如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");
}

【讨论】:

  • 我在 do while 循环后添加代码时遇到了一些问题 System.out.println("Input keycode again");字符串 key2 = keycode.nextLine(); if (key1.equals(key2)) { System.out.println("keycode 匹配"); } else { System.out.println("keycode 不匹配");出错
  • 是的,我正在阅读密钥代码两次。检查第一个条目是否与第二个匹配,但不需要再次检查。只是比较 key1 n key2
  • 您必须在同一个 while 循环中执行此操作。使用count变量,如果一个key读取成功,就加1,读取下一个。如果 count == 2 跳出循环。还要在循环之外声明您的 key1key2
【解决方案3】:

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

【讨论】:

    【解决方案4】:
    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);
    

    我设法把代码放在里面,不知道这样做的方法是否正确?

    【讨论】:

    • 您可以使用大小为 2 的字符串数组,并将读取的键存储在该数组中。我将编辑我的代码。
    猜你喜欢
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    • 1970-01-01
    • 2014-05-20
    相关资源
    最近更新 更多