2012-12-02 2 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 잘못)
  • 당신은 당신이 검사하게 한 대신
  • 공의의 자리수로 시작 모든 문자를 스캔 한 후 첫 번째 숫자를 확인하는 대신 자릿수를 확인하는 즉시 자릿수가 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 자 이하
  • 하나 이상의 대문자
  • 하나 이상의 소문자
  • 하나 이상의 숫자
  • 하나 또는 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:"); 
    } 
    } 
}