-1

简单的密码生成器

Random myrand = new Random();
int x = myrand.nextInt(126);
x = x+1;
char c = (char)x;
//this gets the character version of x
for (int mycounter = 0; mycounter < 10; mycounter++)
{
  x = myrand.nextInt(127);
  x = x+1;
  if (x == 32)
  {
   x = 33;
  }
  c = (char)x;
  System.out.print(c);
  }
 System.out.println();

我的错误在哪里?

我该如何解决?

4

1 回答 1

3

目前您正在检测空格字符,但其他不可打印的字符呢?我不希望任何低于 ASCII 32 的密​​码都可以用作密码。

我怀疑你想要类似的东西:

// Remove bit before the loop which used x and c. It was pointless.
Random myrand = new Random();
for (int mycounter = 0; mycounter < 10; mycounter++)
{
    // Range [33, 127)
    int x = myrand.nextInt(127 - 33) + 33;
    char c = (char) x;
    System.out.print(c);
}

上面的代码并没有将“所有不可打印的内容”映射到单个字符,而是通过进一步限制范围来避免首先选择它。

于 2013-05-22T23:54:12.507 回答