【问题标题】:Password w/ Specific Requirements Program密码与/特定要求程序
【发布时间】:2014-11-08 01:10:36
【问题描述】:

我目前正在为我的 Java 编程课程做作业。

书中的问题要求用户创建一个程序,该程序使用 JOptionPane 框向用户询问密码。密码必须介于 6 到 10 个字符之间,并且至少包含一个数字和一个字母。一旦满足此要求,请要求用户验证其密码。如果他们的第二个输入与第一个不匹配,请再次询问原始问题。一旦两个输入匹配,显示一条消息“成功!”。

我已经到了检查字符数的地步,但我一辈子都做不到,弄清楚如何检查数字和字母。我已经尝试过用于搜索数字和字母的嵌套 for 循环,但是当它没有数字或字母时,我无法找到绕过长度要求的方法。这是我当前的代码。

import javax.swing.JOptionPane;
class Password{
   public static void main(String[] args){

      String input1 = "";
      String input2 = "";
      int inputLength;

      do{
         do{

            input1 = JOptionPane.showInputDialog(null, "Enter your password\nIt must be 6 to 10 characters and\nhave at least one digit and one letter");

            inputLength = input1.length();

         }while(inputLength < 6 || inputLength > 10);

         input2 = JOptionPane.showInputDialog(null, "Verify Password");

      }while(!(input1.equals(input2)));

      JOptionPane.showMessageDialog(null, "Success!");

   }
}

【问题讨论】:

  • 感谢您指出这是家庭作业,因此我们可以帮助您找到答案,而不仅仅是提供答案。回答者——请这样做;需要家庭作业帮助的新用户 - 遵循此示例
  • 你有使用正则表达式的经验吗?
  • 您可以使用正则表达式吗?如果是,尝试使用一些基本的正则表达式教程来编写一个简单的正则表达式,否则,尝试迭代 charArray (input1.toCharArray()) 并跟踪是否至少有一个数字和一个字母。

标签: java


【解决方案1】:

你可以处理正则表达式。

  • (?=.*\d) 表示单词的最后一位数字
  • (?=.*[a-zA-Z]) 表示至少一个字母
  • {6,10} 表示 6 到 10 个字符

所以正确的正则表达式是 ((?=.\d)(?=.[a-zA-Z]).{6,10})

现在看看String的`.matches(String regex)方法:)

如果你不能使用正则表达式:

  • 获取 CharArray (input1.toCharArray()) 并进行迭代。
  • 对于每个字符,检查它是数字还是字符
  • 保留 2 个布尔值(例如 num 和 alpha)并在看到字符数时将其设置为 true

然后,看看你的flag,然后测试一下

num &amp;&amp; alpha &amp;&amp; inputLenght &gt; 6 &amp;&amp; inputLenght &lt; 10

编辑:

你可以使用Character.isLetter()Character.isDigit(),我想你现在已经掌握了足够的信息!

【讨论】:

    【解决方案2】:

    想通了!我最终添加了两个 while 循环来检查内部 do 循环内的数字和字母。如果找到正确的东西,我将 while 循环设置为使布尔表达式为假(我知道,它似乎倒退了)。但这使得如果找到正确的字符,do循环将不会再次运行。感谢所有发帖的人,它真的帮助澄清了事情!

    这是完整的代码:

    import javax.swing.JOptionPane;
    class Password{
       public static void main(String[] args){
    
          String input1 = "";
          String input2 = "";
          int inputLength;
          boolean isDigit = true;
          boolean isLetter = true;
          char c = ' ';
          char d = ' ';
          int x = 0;
    
          do{
             do{
    
                input1 = JOptionPane.showInputDialog(null, "Enter your password\nIt must be 6 to 10 characters and\nhave at least one digit and one letter");
    
                inputLength = input1.length();
    
                while(x < input1.length()){
                   c = input1.charAt(x);
    
                   if(Character.isDigit(c)){
                      isDigit = false;
                      break;
                   }
                   x++;
                }
    
                x = 0;
                while(x < input1.length()){
                   d = input1.charAt(x);
    
                   if(Character.isLetter(d)){
                      isLetter = false;
                      break;
                   }
                   x++;
                }
    
             }while(inputLength < 6 || inputLength > 10 || isDigit || isLetter);
    
             input2 = JOptionPane.showInputDialog(null, "Verify Password");
    
          }while(!(input1.equals(input2)));
    
          JOptionPane.showMessageDialog(null, "Success!");
    
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多