【问题标题】:Using stack to check equal number of letters and digits in password使用堆栈检查密码中相同数量的字母和数字
【发布时间】:2017-02-25 19:56:16
【问题描述】:

我必须编写一个程序来检查密码,密码应该是这样的

  • 至少应包含 8 个字符
  • 只包含字母和数字(特殊字符)
  • 包含相同数量的字母和数字

程序应检查它是否有效并显示适当的消息。 此外,只有堆栈类应该用作数据结构。

这是我目前的想法:

public class dsa_1c {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner sc= new Scanner(System.in);

        String pass;

        System.out.println("Enter a password");
        pass= sc.nextLine();

        if(checkPass(pass)){
            System.out.println("Valid Password");
        }
        else{
            System.out.println("Invalid Password");
        }
    }


        public static boolean checkPass(String password){

            Stack<Character> stack= new Stack<Character>();


            int count1=0, count2=0;

            if(password.length()<8){
                return false;
            }
            else{
             for(int i=0; i<password.length();i++){
                 char c= password.charAt(i);

                  if (!Character.isLetterOrDigit(c)){
                    return false;}

                  if(Character.isLetterOrDigit(c)){

                    stack.push(c);

                  }

                    char top= stack.peek();

                  if(Character.isLetter(top)){
                      count1++;
                      stack.pop();                    

                  }

                  else if(Character.isDigit(top)){                  
                      count2++;
                      stack.pop();                    

                  }

                  if(count1==count2){
                 return true;
                  }

                  if(!stack.isEmpty()){
                 return false;
                  }

             }

            }
                return true;

            }

    }

程序在运行时显示“有效密码”对于我输入的任何超过 8 个字符且没有特殊字符的密码。 这显然是问题所在

if(count1==count2){
   return true;}

因为当我改变它时

if(count1!=count2)
    return false; }

它返回任何有效密码的无效密码。

【问题讨论】:

    标签: java passwords stack


    【解决方案1】:

    最终导致问题的是return true。您可以使用return count1 == count2;,而不是比较两个计数并返回值。下面是一个例子:

    public static boolean checkPass(String password) {
    
        Stack<Character> stack = new Stack<Character>();
        int count1 = 0, count2 = 0;
        if (password.length() < 8) {
            return false;
        } else {
            for (int i = 0; i < password.length(); i++) {
                char c = password.charAt(i);
    
                if (!Character.isLetterOrDigit(c)) {
                    return false;
                } else {
                    stack.push(c);
                }
            }
            while(!stack.isEmpty()){
                char c = stack.pop();
                if(Character.isLetter(c)){
                    count1++;
                }else{
                    count2++;
                }
            }
            return count1 == count2;
        }
    }
    

    【讨论】:

    • 感谢您完善代码。你能解释一下return count1==count2;是如何进行布尔检查的吗?不太明白。
    • count1==count2 返回一个布尔值,类似于if(count1==count2){ return true;} else{return false;}
    【解决方案2】:

    对我来说,使用堆栈似乎有点过头了——迭代字符、计算数字和字母并确保它们具有相同的数字就足够了:

    private static boolean isValidPassword(String password) {
        int letterCount = 0;
        int digitCount = ;
        for (int i = 0; i < password.length(); ++i) {
            char c = password.charAt(i);
            if (Character.isLetter(c)) {
                ++letterCount;
            } else if (Character.isDigit(c)) {
                ++digitCount;
            } else {
                return false;
            }
        }
        return (letterCount + digitCount) >= 8 &&
               letterCount == digitCount;
    }
    

    【讨论】:

      【解决方案3】:

      假设这只是一个操作堆栈的练习,这是我的 2 美分:

      public class dsa_1c {
      public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          System.out.println("Enter a password");
          String pass = sc.nextLine();
      
          if (checkPass(pass)) {
              System.out.println("Valid Password");
          } else {
              System.out.println("Invalid Password");
          }
      }
      
      private static boolean checkPass(String pass) {
          boolean result = false;
          if (pass.length() == 8) {
              Stack stack = new Stack();
              for (char current : pass.toCharArray()) {
                  if (stack.isEmpty()) {
                      stack.push(current);
                      continue;
                  }
                  char previousChar = stack.pop();
                  if (sameType(current, previousChar)) {
                      stack.push(previousChar);
                      stack.push(current);
                  }
              }
              if (stack.isEmpty()) {
                  result = true;
              }
          }
          return result;
      }
      
      public static boolean sameType(char current, char previousChar) {
          boolean result = false;
          if (Character.isAlphabetic(current) && Character.isAlphabetic(previousChar)) {
              result = true;
          }
          else if (Character.isDigit(current) && Character.isDigit(previousChar)) {
              result = true;
          }
          return result;
      }
      
      static class Stack extends ArrayList {
      
          public static final char NULL_CHARACTER = 0x0;
      
          public void push(Character letter) {
              add(letter);
          }
          public Character pop() {
              Character result = NULL_CHARACTER;
              if (!isEmpty()) {
                  result = (Character)remove(size()-1);
              }
              return result;
          }
      }
      

      }

      【讨论】:

        猜你喜欢
        • 2011-06-20
        • 2016-03-02
        • 2021-03-13
        • 2016-03-22
        • 2018-06-19
        • 1970-01-01
        • 2017-04-25
        • 2017-03-20
        相关资源
        最近更新 更多