我正在制作一个随机密码生成器,并希望将数组值添加到另一个数组" pa"为了使单个字符串密码。任何帮助,将不胜感激。
package project;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.Random;
public class RandomPass {
public static void Randpass(){
Scanner scan = new Scanner(System.in);
Random rand = new Random();
String[] alphabet= {"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"};
JOptionPane.showMessageDialog(null, "Welcome to RandomPassword Generator");
int c= Integer.parseInt(JOptionPane.showInputDialog(null,"How long do you want the password"));
int nc =0-c;
int c2=c/2;
int nc2= 0-c2;
int ncm =(nc+1)/2;
String pa [];
if(c%2==0){
for(int x=nc2;x<0;x++){
int alphanum =rand.nextInt(26);
pa.add(alphabet[alphanum]);
int numNum =rand.nextInt(10);
pa.add(numNum);
}
}else{
for(int x=ncm;x<0;x++){
int alphanum =rand.nextInt(26);
int numNum =rand.nextInt(10);
}
}
}
}
答案 0 :(得分:1)
public class Hello {
private static final String CHAR_LIST = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
private static final int RANDOM_STRING_LENGTH = 10;
public String generateRandomString(){
StringBuffer randStr = new StringBuffer();
for(int i=0; i<RANDOM_STRING_LENGTH; i++){
int number = getRandomNumber();
char ch = CHAR_LIST.charAt(number);
randStr.append(ch);
}
return randStr.toString();
}
private int getRandomNumber() {
int randomInt = 0;
Random randomGenerator = new Random();
randomInt = randomGenerator.nextInt(CHAR_LIST.length());
if (randomInt - 1 == -1) {
return randomInt;
} else {
return randomInt - 1;
}
}
public static void main(String[] args) {
Random rand = new Random();
Hello msr = new Hello();
List<String> list = new ArrayList<String>();
while(true)
{
list.add(msr.generateRandomString());
//System.out.println("happy birthday smash from "+msr.generateRandomString());
}
}
}
我为我的生日做了一些修改,希望我的代码可以正常工作。快乐编码