2012-12-02 46 views
1

問題描述:檢查密碼代碼

某些網站對密碼施加了一定的規則。編寫一個方法來檢查一個字符串是否是一個有效的密碼。假設密碼規則如下:

  • 密碼必須至少包含8個字符。
  • 密碼只包含字母和數字。
  • 密碼必須至少包含兩位數字。

編寫一個程序,提示用戶輸入密碼,如果遵循規則顯示「有效密碼」,否則顯示「無效密碼」。

這是我到目前爲止有:

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

當我運行該程序只檢查密碼的長度,我無法弄清楚如何確保它的字母和數字的檢查,並且密碼中至少有兩位數字。

+0

前值修正爲這是它應該是如果password.length()<= 8,因爲它說「至少8「,這意味着8是有效的。 – ThePerson

+0

@NutterzUK:所以,如果8有效,如果長度<8,則該方法應該返回false。OP得到了正確的結果。 –

+0

你允許使用正則表達式嗎? – Vulcan

回答

1

你幾乎明白了。有一些錯誤,但:

  • 你不遍歷密碼的所有字符(i < password.length() - 1是錯誤的)
  • 你開始的1而不是0
  • 你做檢查的一個數字計數當您遇到第一個數字時,您的數字計數至少爲2,而不是在掃描完所有字符後檢查數字。
0

如前所述,您應該先查看所有密碼字符。計數您的數字,最後檢查計數是否小於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; 
} 
-1
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; 
} 
0

假設的有效密碼:

  • 8個或更多字符,但不能超過16個字符
  • 一個或多個大寫字符
  • 一個或多個小寫字符
  • 一個或多個數字
  • one or m礦特殊字符(如$,@或!)

代碼:

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:"); 
    } 
    } 
}