【问题标题】:Checking Password Code检查密码代码
【发布时间】:2012-11-20 09:34:58
【问题描述】:

问题描述:

某些网站对密码施加了某些规则。编写一个检查字符串是否为有效密码的方法。假设密码规则如下:

  • 密码必须至少包含八个字符。
  • 密码仅由字母和数字组成。
  • 密码必须至少包含两位数字。

编写一个程序,提示用户输入密码,如果遵守规则则显示“有效密码”,否则显示“无效密码”。

这是我目前所拥有的:

import java.util.*;  
import java.lang.String;  
import java.lang.Character;  

/**
 * @author CD
 * 12/2/2012
 * This class will check your password to make sure it fits the minimum set     requirements.
 */
public class CheckingPassword {  

    public static void main(String[] args) {  
        Scanner input = new Scanner(System.in);  
        System.out.print("Please enter a Password: ");  
        String password = input.next();  
        if (isValid(password)) {  
            System.out.println("Valid Password");  
        } else {  
            System.out.println("Invalid Password");  
        }  
    }  

    public static boolean isValid(String password) {  
        //return true if and only if password:
        //1. have at least eight characters.
        //2. consists of only letters and digits.
        //3. must contain at least two digits.
        if (password.length() < 8) {   
            return false;  
        } else {      
            char c;  
            int count = 1;   
            for (int i = 0; i < password.length() - 1; i++) {  
                c = password.charAt(i);  
                if (!Character.isLetterOrDigit(c)) {          
                    return false;  
                } else if (Character.isDigit(c)) {  
                    count++;  
                    if (count < 2)   {     
                        return false;  
                    }     
                }  
            }  
        }  
        return true;  
    }  
}

当我运行程序时,它只检查密码的长度,我不知道如何确保它同时检查字母和数字,以及密码中至少有两位数字。

【问题讨论】:

  • 对此的第一个更正是如果password.length()
  • @NutterzUK:因此,如果 8 有效,则该方法应在长度
  • 你可以使用正则表达式吗?

标签: java passwords bluej


【解决方案1】:

假设一个有效的密码有:

  • 8 个或更多字符,但不超过 16 个字符
  • 一个或多个大写字符
  • 一个或多个小写字符
  • 一位或多位数字
  • 一个或多个特殊字符(如 $、@ 或 !)

代码:

import java.util.Scanner;
public class Password {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int min =8;
    int max=16;
    int digit=0;
    int special=0;
    int upCount=0;
    int loCount=0;
    String password;
    Scanner scan = new Scanner(System.in);
    System.out.println(" Enter Your Password:");
        password = scan.nextLine();
    if(password.length()>=min&&password.length()<=max){
        for(int i =0;i<password.length();i++){
            char c = password.charAt(i);
            if(Character.isUpperCase(c)){
                upCount++;
            }
            if(Character.isLowerCase(c)){
                loCount++;
            }
            if(Character.isDigit(c)){
                digit++;
            }
            if(c>=33&&c<=46||c==64){
                special++;
            }
        }
        if(special>=1&&loCount>=1&&upCount>=1&&digit>=1){
            System.out.println(" Password is good:");
        }

    }

    if(password.length()<min){

        for(int i =0;i<password.length();i++){
            char c = password.charAt(i);
            if(Character.isLowerCase(c)){
                loCount++;
            }
            }

        if(loCount>0){
            System.out.println(" Password must be atleat "+min+" characters:");
            System.out.println(" You need atleast one upper case chracter:");
            System.out.println(" You need atleast one digit:");
            System.out.println(" You need atleast one special chracter:");



    }
    }
    else if(password.length()<min&&upCount>1){
        for(int i =0;i<password.length();i++){
        char c =password.charAt(i);
        if(Character.isLowerCase(c)){
            loCount++;
        }
         if(Character.isUpperCase(c)){
            upCount++;
        }
        }
        if(loCount>0&&upCount>0){
        System.out.println(" Password must be atleast "+min+" chracters:");
        System.out.println(" You need atleast one digit:");
        System.out.println(" You need atleast one special chracter:");
    }
    }
     if(password.length()>max||password.length()>=max&&upCount>1&&loCount>1&&digit>1){
         System.out.println(" Password is too long.Limit is "+max+" chracters:");
                 System.out.println(" You need atleast one special chracter:");

        }
      if(password.length()>=min&&password.length()<=max&&loCount>0&&upCount>0&&digit>0&&special==0){
         System.out.println(" You need atleast a special chracter");
     }
      if(password.length()>=min&&password.length()<=max&&loCount>0&&upCount>0&&digit==0&&special==0){
         System.out.println(" You need atleast one digit:");
         System.out.println(" You need atleast one special chracter:");
     }
   }
}

【讨论】:

    【解决方案2】:

    你几乎明白了。但是有一些错误:

    • 您没有遍历密码的所有字符(i &lt; password.length() - 1 错误)
    • 您从 1 而不是 0 开始计数
    • 您在遇到第一个数字时立即检查位数是否至少为 2,而不是在扫描完所有字符后进行检查

    【讨论】:

      【解决方案3】:

      如前所述,您应该首先检查所有密码字符。数一数你的数字,最后检查 count 是否小于 2。 这是参考代码。

      if (password.length() < 8) {   
              return false;  
          } else {      
              char c;  
              int count = 0;   
              for (int i = 0; i < password.length(); i++) {  
                  c = password.charAt(i);  
                  if (!Character.isLetterOrDigit(c)) {          
                      return false;  
                  } else if (Character.isDigit(c)) {  
                      count++;     
                  }  
              }  
              if (count < 2)   {     
                  return false;  
              }  
          }  
          return true;  
      }  
      

      【讨论】:

        【解决方案4】:
        public void run()
        {
            String password= readLine("Insert Password: ");
            boolean len= true;
            boolean letter= true;
            boolean twodig= true;
               if (password.length() < 8) {   
                 len = false;  
            } else {      
                char c;  
                int count = 0;   
                for (int i = 0; i < password.length(); i++) {  
                    c = password.charAt(i);  
                    if (!Character.isLetterOrDigit(c)) {          
                        letter = false;  
                    } else if (Character.isDigit(c)) {  
                        count++;     
                    }  
                }  
                if (count < 2)   {     
                    twodig = false;  
                }  
            }  
            if(len ==true && letter == true && twodig == true)
            {
                System.out.println("This password is valid ");
            }
            else
            {
                System.out.println("This password is invalid");
            }
        }
        

        【讨论】:

          【解决方案5】:

          封装方法;

          /* 2.编写一个Java方法来检查一个字符串是否有效 密码。 密码规则: 密码必须至少有十个字符。 密码仅由字母和数字组成。 密码必须至少包含两位数字。

          预期输出:

          1. 密码必须至少包含八个字符。
          2. 密码仅由字母和数字组成。
          3. 密码必须至少包含两位数字
            输入密码(您同意以上条款。):abcd1234
            密码有效:abcd1234 */
          public class CheckPassword {
              
              public static String password;
              public static int disitCounter = 0;
              
              public static boolean isValid(String password) {
                  
                  
                  if (password.length() >= 10 ) {
                      for(int index = 0; index < password.length(); index++) {
                          char passChar = password.charAt(index);
                          if (!Character.isLetterOrDigit(passChar)) {
                              return false;
                          }
                          else {
                              if (Character.isDigit(passChar)) {
                                  disitCounter++;
                              }
                          }
                          
                      }
                      
                  }
                  if(disitCounter < 2) {
                      return false;
                  }
                  return true;
              }
          
              public static void main(String[] args) {
                  password = "abcdefgh1w3";
                  if(isValid(password)) {
                      System.out.print("It is a valid password");
                  }
                  else {
                      System.out.print("It is a invalid password");
                  }
              }
          }
          

          【讨论】:

            【解决方案6】:
            package com.parag;
            
            /*
             * @author Parag Satav
             */
            public boolean check(String password) {
            
                boolean flagUppercase = false;
                boolean flagLowercase = false;
                boolean flagDigit = false;
                boolean flag = false;
            
                if (password.length() >= 10) {
            
                    for (int i = 0; i < password.length(); i++) {
            
                        if (!Character.isLetterOrDigit(password.charAt(i))) {
                            return false;
                        }
            
                        if (Character.isDigit(password.charAt(i)) && !flagDigit) {
                            flagDigit = true;
            
                        }
            
                        if (Character.isUpperCase(password.charAt(i)) && !flagUppercase) {
                            flagUppercase = true;
            
                        }
            
                        if (Character.isLowerCase(password.charAt(i)) && !flagLowercase) {
                            flagLowercase = true;
                        }
                    }
                }
            
                if (flagDigit && flagUppercase && flagLowercase) {
                    flag = true;
                    System.out.println("Success..");
                } else
                    System.out.println("Fail..");
            
                return flag;
            }
            

            【讨论】:

              猜你喜欢
              • 2017-09-24
              • 2018-05-19
              • 1970-01-01
              • 2021-07-13
              • 1970-01-01
              • 2014-02-09
              • 2011-06-20
              • 2017-11-13
              相关资源
              最近更新 更多