import java.util.Random;
import java.util.Scanner;
public class PassGen {
public static void main(String[] args) {
String[] characters = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"};
StringBuilder b = null;
Scanner scan = new Scanner(System.in);
System.out.println("Enter password length.");
int length = scan.nextInt();
while (length > 20 || length < 6) {
System.out.println("Password must be between 6 and 20 characters long.");
length = scan.nextInt();
}
Random rand = new Random();
for (int i = 0; i <= length; i++) {
int x = rand.nextInt(characters.length) + 1;
b = new StringBuilder(length + 1);
String s = characters[x];
b.append(s);
}
System.out.println("Your password is: " + b.toString());
}
}
何らかの理由で、このプログラムを実行すると、入力された長さに関係なく、単一のランダムな文字が表示される前に、for ループが 1 回だけ実行されます。