2011-02-03 3 views
1

편집 - 버전 첫 번째 게시물은 confusamagin이었습니다. 나의 임무는 암호 프롬프트 프로그램을 만드는 것이다. 암호가 적어도 하나의 숫자와 하나의 문자가 있는지 확인하려면 암호를 검사해야합니다. 또한 암호 길이는 6 - 10 사이 여야합니다.자바 코드 문자 및 숫자의 암호를 확인 하시겠습니까?

내 문제는 숫자와 문자가 암호가 있는지 확인하는 방법입니다. 비밀 번호 확인 영역에서 나는 정말로 어디에서 시작해야할지 모르겠습니다. 문자와 숫자가 하나 인 지 확인하는 방법을 모르겠습니다. 중 하나를 수행하는 방법이나 for 문을 사용하여 계산하고 확인하는 방법을 알고 있지만 모든 문자 또는 모든 숫자가 포함되어 있는지 확인하는 것입니다. 다음은

잘못 두 가지가 있습니다

import java.util.Scanner; 

class Password { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

//------ENTER A USERNAME 
     System.out.println("Welcome please enter your username and password."); 
     System.out.print("Username >>"); 
     input.nextLine(); 


//------PASSWORD AUTHENTICATION BEGIN  
     String password = enterPassword(); 
      while (!checkPassword(password)) { 
      System.out.println("Password must be 6 - 10 characters long!"); 
      password = enterPassword(); 
      } 

//------PASSWORD VERIFY 
     String passwordverify = enterPassword(); 
     while (!password.equals(passwordverify)){ 
      System.out.println("ERROR - Passwords DO NOT MATCH Re-Enter Passwords Again"); 
      password = enterPassword(); 

     } 

//------ACCEPT PASSWORD  
     System.out.println("Username and Password Accepted!"); 

     } 


//--ENTER PASSWORD STATEMENT 
    public static String enterPassword(){ 
     String password; 
     Scanner input = new Scanner(System.in); 
     System.out.print("Password >>"); 
     password = input.nextLine(); 
     return password; 
     } 

//--BOOLEAN CHECK PW  
    public static boolean checkPassword(String password){ 
     int length; 
     length = password.length(); 
      if (length < 6 || length > 11){ 
      return false; 
      } 
        for (int i = 0; i < password.length();i++){ 
     if (!Character.isLetter(password.charAt(i))) 
     return false; 
      }   
      return true; 
     } 

} 
+0

질문은 무엇인가 : 당신이해야 할 일은

뭔가처럼? – jzd

+0

@jzd : 제목은 공짜입니다 (몸에 어딘가에 질문이 있어야한다는 것에 동의하지만). – spender

+0

@spender 제목은 힌트를 주지만 몇 가지 루프와 논리 검사가 있습니다. 질문은 매우 모호합니다. 코드에서 여러 문제를 발견하는 데 오랜 시간이 걸리지 않습니다. – jzd

답변

0

... 내가 지금까지있는 것입니다.

  1. 첫 번째 문자가 아닌 문자로 문자 검사가 실패합니다. 귀하의 자릿수 검사와 동일한 문제가 발생합니다. 모든 문자가 비 문자 일 경우에만 거부하려고합니다. 그래서 논리 오류.

  2. 세 개의 루프가 있습니다. 나쁜 생각. 길이 검사를 한 번 통과하면 결코 다시 검사하지 않을 것이기 때문입니다. 누군가가 입력하면 어떻게되는지 생각해보십시오 : 12345678. 길이는 괜찮지 만 편지는 없습니다. Ok, 이제 다음을 입력하십시오. a1. 길이가 다시 확인되지 않았습니다. 모든 문자는 문자/숫자가 각각있는 경우

+0

나는 또한 모든 것을 두 번하는 것을 피하기 위해 ... 사용하고 싶습니다. – lll

1

checkPasswordLettercheckPasswordDigit는 true를 돌려줍니다. 이게 당신이 의미 한거야? 여기

+0

아니, 그걸 지적 해 주셔서 감사합니다. 이제는 숫자 나 문자가 있는지 확인하는 방법을 찾아야합니다. 내 책이 그것을 할 수있는 방법을 제공하지 않기 때문에 여전히 그것에 매달렸다. – allencoded

5
public static boolean checkPasswordLetter(String password){ 
      for (int i = 0; i < password.length();){ 
      if (!Character.isLetter(password.charAt(i))){ 
       return false; 
       } 
      } 
     return true; 
     } 

같은과 checkPasswordDigit

문자가없는 경우 당신은 내가 ++ 또는 루프가 영원히 것입니다 위해 내가에 필요한 변수를 증가하지 않았다
+0

잘자요. 완전히 놓쳤습니다. – spender

+0

당신이 무슨 뜻인지 알기 때문에 나는 for를 ++에 추가했습니다. 그러나 추가 조사는 전체 문자열을 문자로 검사하기 때문에 내가 원하는 것을하지 않습니다. – allencoded

1

우선 ... 그것은 자바가 아니라고 아니다 반복 또는 부울 검사. 자바는 당신이하고 싶은 말을하고 있습니다. 이제, 당신이 원하는 무엇

있습니다이 뭘 다릅니다.

public static void main(String[] args) { 
    // ... 
    String password = enterPassword(); 
     while (!isPasswordValid(password)) {    
     password = enterPassword(); 
    } 
    System.out.println("Username and Password Accepted!"); 
} 

public static boolean isPasswordValid(String password) { 
    // return true if and only if password: 
    // 1. has 6-10 characters 
    // 2. contains at least one digit 
    // 3. contains at least one character 
    // print messages accordingly 
} 
+0

+1 의사 결정을 내리지 않도록 의사 코드 접근법을 좋아합니다. – spender

+0

멋진 사랑. 이 비트는 대단히 도움이되었습니다. 사실 지금 checkPassword로 변경했습니다. – allencoded

0
import java.util.Scanner; 

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

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) { 
     if (password.length() < 9) { 
      return false; 
     } 

else { 
     char c = 0 ; 
       int count=0; 
       System.out.println(password.length()); 
     for (int i = 0;i<=password.length()-1; i++) 
       { 
c = password.charAt(i); 
System.out.println(c); 
if (!Character.isLetterOrDigit(c)) 
{  
return false;   
} 
else if (Character.isDigit(c)) 
{ 
count++;  
if(count==3) 
{ 
return false; 
} 
} 
} 
       return true; 
} 
       } 

       } 
0
import java.util.Scanner; 
public class password{ 
public static void main(String[] args) { 
    // Create a Scanner 
    Scanner input = new Scanner(System.in); 

    // Prompt the user to enter a string 
System.out.print("Enter a password ") ; 
    String s = input.nextLine(); 


if(isValid(s)) 
System.out.println(s + " is a valid password"); 
else 
System.out.println(s + " is not a valid password"); 
} 

public static boolean isValid(String s) 
{ 

    int i = 0,j=0; 


if(s.length()>=8) 
    { 
    for(i=0;i<s.length();i++) 
    { 
    //if(Charcter.isLetter(s.charAt(i))||Digit.isLetter(s.charAt(i))) 
    if (Character.isLetterOrDigit(s.charAt(i))) 
{ 
    if(Character.isDigit(s.charAt(i))) 
    j++; 
} 
    } 
    } 
    else 
    return false; 
    if(j>=2) 
    return true; 
     return false; 
    } } 
관련 문제