Java - 密码生成器

时间:2015-10-19 22:42:16

标签: java

我这样做是一个项目。其重点是创建一个密码,其中包含您在所选长度中选择的字符类型。但是当我使用选项D时,它不会返回任何内容以及选项B和C,无论我输入的长度如何,我总是得到一个随机数字的字符。如果你可以运行它,并指出我正确的方向,这将是非常感谢。

import java.util.Scanner;

public class Password
{
    public static void main(String [] args){
        String password = "";
        String temp = "";
        int randLetter = 0;
        int randNumber = 0;
        int randPunct = 0;
        int charSelection = 0;
        int counter = 0;
        String lowerCase = "abcdefghijklmnopqrstuvwxyz";
        String upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String number = "0123456789";
        String punctuation = "!?";


        Scanner in = new Scanner(System.in);

        System.out.println("Please choose what characters you would like to use.");
        System.out.println("[A] Lowercase Letters");
        System.out.println("[B] Lowercase & Uppercase Letters");
        System.out.println("[C] Lowercase, Uppercase, Numbers");
        System.out.println("[D] Lowercase, Uppercase, Numbers, Punctuation");
        System.out.print("Selection: ");
        String selection = in.next();

        System.out.println();
        System.out.print("Select a length (1 - 14): ");
        int length = in.nextInt();

        if(selection.equalsIgnoreCase("A")){
            for (int i = 0; i < length; i++){
                randLetter = 1 + (int)(Math.random() * 26); 
                temp = lowerCase.substring(randLetter - 1 , randLetter); 
                password += temp;    
            }
        }
        else if(selection.equalsIgnoreCase("B")){
            while (counter < length){
                for (int i = 0; i < length; i++){

                    charSelection = 1 + (int)(Math.random() * 10); 

                    if (charSelection < 5){

                        randLetter = 1 + (int)(Math.random() * 26); 
                        temp = lowerCase.substring(randLetter - 1 , randLetter);
                        counter++;
                        password += temp;  
                    }
                    else if (charSelection > 5 && charSelection < 10){

                        randLetter = 1 + (int)(Math.random() * 26); 
                        temp = upperCase.substring(randLetter - 1 , randLetter);
                        counter++;
                        password += temp;  
                    }
                }
            }
        }
        else if(selection.equalsIgnoreCase("C")){
            while (counter < length){
                for (int i = 0; i < length; i++){

                    charSelection = 1 + (int)(Math.random() * 17);
                    randNumber = 1 + (int)(Math.random() * 9);

                    if (charSelection < 5){

                        randLetter = 1 + (int)(Math.random() * 26); 
                        temp = lowerCase.substring(randLetter - 1 , randLetter);
                        counter++;
                        password += temp;
                    }
                    else if (charSelection > 5 && charSelection < 10){

                        randLetter = 1 + (int)(Math.random() * 26); 
                        temp = upperCase.substring(randLetter - 1 , randLetter);
                        counter++;
                        password += temp;
                    }
                    else if (charSelection > 15 && charSelection < 17){

                        randLetter = 1 + (int)(Math.random() * 26); 
                        temp = number.substring(randNumber - 1 , randNumber);
                        counter++;
                        password += temp;
                    }
                }
            }
        }
        else if(selection.equalsIgnoreCase("D")){
            while (counter < length){
                for (int i = 1; i < 0; i++){

                    charSelection = 1 + (int)(Math.random() * 20); 
                    randNumber = 1 + (int)(Math.random() * 9);
                    randPunct = 1 + (int)(Math.random() * 2);
                    if (charSelection < 5){

                        randLetter = 1 + (int)(Math.random() * 26); 
                        temp = lowerCase.substring(randLetter - 1 , randLetter);
                        counter++;
                        password += temp;
                    }
                    else if (charSelection > 5 && charSelection < 10){

                        randLetter = 1 + (int)(Math.random() * 26); 
                        temp = upperCase.substring(randLetter - 1 , randLetter);
                        counter++;
                        password += temp;
                    }
                    else if (charSelection > 15 && charSelection < 17){

                        randLetter = 1 + (int)(Math.random() * 26); 
                        temp = number.substring(randNumber - 1 , randNumber);
                        counter++;
                        password += temp;                 
                    }
                    else if (charSelection > 17){

                        randLetter = 1 + (int)(Math.random() * 26); 
                        temp = punctuation.substring(randPunct - 1 , randPunct);
                        counter++;
                        password += temp;   
                    }
                } 
            }
        }
        System.out.println(password);
    }
}

2 个答案:

答案 0 :(得分:1)

for (int i = 1; i < 0; i++){

这种情况永远不会成立,所以循环体永远不会执行。

目前还不清楚你真正想要的是什么,所以很难建议正确的条件 - 你的意思是length而不是0,就像你在其他循环中那样吗?

答案 1 :(得分:0)

模式“B”和“C”:
此循环:while (counter < length){将再次运行密码生成,如果此行中至少生成一个值:

charSelection = 1 + (int)(Math.random() * 10); 

等于5(模式“B”),或者等于5或者在(包括)10和15之间(模式“C”)。所以基本上你尝试length次来生成一个新值,然后重复这个过程,直到你生成了比counter更多的字符(length),这很可能会产生超过length的字符。 {1}}字符。我建议您删除未使用的值并删除while - 循环以提供一致的行为。

模式“D”:

for (int i = 1; i < 0; i++){

此循环将终止生成,而不会生成单个字符,因为i < 0未在循环开始处保持(i初始化为1)。

并建议: 我建议你将代码分成几个部分,因为有大量可重用的代码:

Random r = new Random();

public char nextChar(String valid_chars){
    return valid_chars.charAt(r.nextInt(valid_chars.length()));
}

public String generate(String chars , int len){
    String tmp = "";

    for(int i = 0 ; i < len ; i++)
        tmp += nextChar(chars);

    return tmp;
}

public static void main(String[] args){
    ...
    switch(selection.toLowerCase()){
        case "a":
        {
            String password = generate("abcdefghijklmnop..." , length);
            System.out.println(password);
        }
        break;
        ...
    }
    ...
}

这有助于保持代码的可读性,可维护性,并允许简单扩展到其他模式。