-2

我写了一个程序,我想要一些以大写字母+数字开头的输出。然后从允许的字符中随机生成一些。

但我发现输出包含如下空格:k r w ea. 而我期望:W3krwea

我不知道为什么这些空格会出现在我的输出中。

import java.util.Random;
import sun.security.util.Password;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author taskin
 */

    public class RPG
{

    public static String generate(String allowedCharacters,
                                  boolean mustHaveUppercase, boolean mustHaveDigit,
                                  int minLength, int maxLength)
    {
        Random rand=new Random();
        String rp = null; // Random Password
        int Length=allowedCharacters.length();
        char[] password=new char[25];
        int [] asc=new int[18];
        char[] UpperCase=new char[25];
        char[] Digit=new char[25];
        char[] Mixed=new char[25];
        int point1=0,point2=0,point3=0;

        for (int i=0;i<Length;i++)
        {
            asc[i]=(int)allowedCharacters.charAt(i);

        }
        for (int k=0;k<Length;k++)
        {
            if (asc[k]>=65 && asc[k]<=90)
            {
                UpperCase[point1]=(char)asc[k];
                point1++;
                continue;
            }
            else if (asc[k]>=48 && asc[k]<=57)
            {
                Digit[point2]=(char)asc[k];
                point2++;
                continue;
            }
            else
            {
                Mixed[point3]=(char)asc[k];
                point3++;
            }

        }
        StringBuilder strbld=new StringBuilder();
        int length=maxLength-minLength+1+minLength;
        strbld.append(UpperCase[rand.nextInt(UpperCase.length)]);//get a uppercase char
        strbld.append(Digit[rand.nextInt(Digit.length)]);//get a digit
        for(int i=0;i<length-2;i++){
            strbld.append(Mixed[rand.nextInt(Mixed.length)]);
        }
        rp=strbld.toString();

        // Your code to set rp appropriately goes here

        return rp;
    }

    public static void main(String[] args)
    {
        String allowedCharacters = "weakPasSWorD1234$*";
        boolean mustHaveUppercase = true;
        boolean mustHaveDigit = true;
        int minLength = 3;
        int maxLength = 20;
        String randomPassword = generate(allowedCharacters,
                                         mustHaveUppercase, mustHaveDigit, minLength,
                                         maxLength);
        System.out.println("Random password: " + randomPassword);
    }
}
4

1 回答 1

1

.length您正在使用的内容将返回数组的总长度。Char大约是 25 左右。但实际上,您的长度约为 4-5 个字符。所以,它会产生一些空格。注意:功能不一样.length()(我认为你误解了。)

请查看线程以使概念清晰。

使用您在循环中使用的相同长度。将块更改StringBuilder为:

    StringBuilder strbld=new StringBuilder();
    int length=maxLength-minLength+1+minLength;
    System.out.println("n     "+strbld.append(UpperCase[rand.nextInt(point1)]));//;

    //get a uppercase char
    strbld.append(Digit[rand.nextInt(point2)]);
    for(int i=0;i<length-2;i++){
        strbld.append(Mixed[rand.nextInt(point3)]);

输出:

  W1aearere$karasa$akke

没有空白。

于 2013-01-22T17:56:17.443 回答