2016-07-04 44 views
0

我有依靠四个字符类型,以确定密码是否四类该密码系统:需要改进的密码系统catagorization

boolean upper=false; 
    boolean lower=false; 
    boolean number=false; 
    boolean symbol=false; 

    for(int i=0; i<password.length(); i++) 
    { 
    character=password.charAt(i); 

    if ((character>=65)&&(character<=90)){ 
    System.out.println("This character is a uppercase letter:" +character); 
    upper=true;} 
    if ((character>=97)&&(character<=122)){ 
    System.out.println("This character is a lowercase letter:" +character); 
    lower=true;} 
    if ((character>=48)&&(character<=57)){ 
    System.out.println("This character is a number:" +character); 
    number=true;} 
    if (((character>=33)&&(character<=47))||((character>=58)&&(character<=64))||((character>=93)&&(character<=96))||((character>=123)&&(character<=126))){ 
    System.out.println("This character is a symbol:" +character); 
    symbol=true;} 
    } 

这是我把他们分为四类:弱,中,强,以及超强:

boolean weak=false; 
    boolean medium=false; 
    boolean strong=false; 
    boolean superstrong=false; 


     int level = 0; 

    if((upper=true) || (lower=true) || (number=true) || (symbol=true)) { 
     weak=true; 
      level++;} 

    if (((upper=true) && (lower=true)) || ((upper=true) && (number=true)) || ((upper=true) && (symbol=true)) || ((lower=true) && (symbol=true)) || ((lower=true) && (number=true)) || ((number=true) && (symbol=true))){ 
     medium=true; 
      level++;} 

    if(((upper=true) && (lower=true) && (number=true)) || ((upper=true) && (lower=true) && (symbol=true)) || ((upper=true) && (number=true) && (symbol=true)) || ((lower=true) && (number=true) && (symbol=true))){ 
     strong=true; 
      level++;} 

    if((upper=true) && (lower=true) && (number=true) && (symbol=true)){ 
     superstrong=true; 
      level++;} 

    if(level >= 1){ 
     System.out.println("This password is weak ");} 
    if(level >= 2){ 
     System.out.println("This password is medium.");} 
    if(level >= 3){ 
     System.out.println("This password is strong.");} 
    if(level >= 4){ 
     System.out.println("This password is super strong.");} 
    } 

的问题是,它编译没有错误,并且密码检查检查密码,以正确的类别,但问题是,无论多么人物的多种类型的有中密码,类别将永远是b e作为SUPERSTRONG输出。我已经尝试过在这个系统中所有小写密码和所有其他类型的密码,但这总是会发生。 我该如何解决这个问题?

下面是完整的代码,如果有帮助:

如果
import java.util.Scanner; 

public class passwordChecker 
{ 

public static void main(String [] args) 
{ 
boolean repeat=true; 
String password; 
int length; 

while(repeat==true){ 

System.out.println ("Please enter your password."); 
Scanner scan = new Scanner(System.in); 
password=scan.nextLine(); 
length=password.length(); 

if(length<=6 | length>=12) { 
System.out.println("Your password does not meet the requirements. 
Please enter a new password."); 
} 
else { 
System.out.println("Your password meets the criteria."); 
char character='\0'; 
    boolean upper=false; 
    boolean lower=false; 
    boolean number=false; 
    boolean symbol=false; 

    for(int i=0; i<password.length(); i++) 
    { 
    character=password.charAt(i); 

    if ((character>=65)&&(character<=90)){ 
    System.out.println("This character is a uppercase letter:"     +character); 
    upper=true;} 
    if ((character>=97)&&(character<=122)){ 
    System.out.println("This character is a lowercase letter:" +character); 
    lower=true;} 
    if ((character>=48)&&(character<=57)){ 
    System.out.println("This character is a number:" +character); 
    number=true;} 
    if (((character>=33)&&(character<=47))||((character>=58)&&`enter code here`(character<=64))||((character>=93)&&(character<=96))||`enter code here`((character>=123)&&(character<=126))){ 
    System.out.println("This character is a symbol:" +character); 
    symbol=true;} 
    } 
    boolean weak=false; 
    boolean medium=false; 
    boolean strong=false; 
    boolean superstrong=false; 


     int level = 0; 

    if((upper=true) || (lower=true) || (number=true) || (symbol=true)) { 
     weak=true; 
      level++;} 

    if (((upper=true) && (lower=true)) || ((upper=true) && (number=true)) || ((upper=true) && (symbol=true)) || ((lower=true) && (symbol=true)) || ((lower=true) && (number=true)) || ((number=true) && `enter code here`(symbol=true))){ 
     medium=true; 
      level++;} 

    if(((upper=true) && (lower=true) && (number=true)) || ((upper=true) && (lower=true) && (symbol=true)) || ((upper=true) && (number=true) && (symbol=true)) || ((lower=true) && (number=true) && (symbol=true))){ 
     strong=true; 
      level++;} 

    if((upper=true) && (lower=true) && (number=true) && (symbol=true)){ 
     superstrong=true; 
      level++;} 

    if(level >= 1){ 
     System.out.println("This password is weak ");} 
    if(level >= 2){ 
     System.out.println("This password is medium.");} 
    if(level >= 3){ 
     System.out.println("This password is strong.");} 
    if(level >= 4){ 
     System.out.println("This password is super strong.");} 
    } 



System.out.println("Do you want to enter another password?"); 
Scanner scan2 = new Scanner (System.in); 
String choice=scan2.nextLine(); 
if(choice.equals("yes")||choice.equals("Yes")) repeat = true; 
else{ 
repeat=false; 
}}}} 

对不起的代码看起来像它不应该。如果你照常输入,它应该工作得很好。

+0

这个'如果(..上=真...)'第一将'true'赋给'upper'然后测试'upper'的值(现在显然是这样)。如果你想比较使用'=='运算符(或者根本不使用'==',简单的'if(boolValue)'和'if(boolValue == true)'相同,并且会阻止你做这个有些错误,否则只需使用'if(!boolValue)')。 – Pshemo

+2

这种方法存在太多的错误,以便在不完全重写的情况下使其值得“修复”。你可以做的最有教育意义的事情就是在你的IDE调试器中逐步查看它的功能,我保证会为你提供一些惊喜。代码的行为并不像你认为的那样,你必须通过它来了解实际发生的事情。此外,根据密码强度是否包含某些字符类来对密码强度进行分类本身就是一个弱测试。你需要研究基础信息理论并学习“熵”。 –

回答

1

当你

upper=true 

要指定uppertrue

所以,当你做

if (upper=true) 

这是一样的

upper=true; 
if (upper) 

我怀疑你想用==比较然而一个更好的解决方案是使用布尔作为条件。

if (upper || lower || number || symbol) { 

BTW更好的方法来检查字符是

if (Character.isUppercase(ch)) { 
    // upper case 
} else if (Character.isLowercase(ch)) { 
    // is lower 
} else if (Character.isDigit(ch)) { 
    // is digit 
} else { 
    // is symbol 
} 

和水平,你可以做

int level = (upper?1:0) + (lower?1:0) + (number?1:0) + (symbol?1:0);