La seguente classe mi stampa 10 password uguali mentre io vorrei che venissero stampate 10 password differenti.
dove ho sbagliato?
codice:import java.util.*; public class PwdGenerator { public static void main(String[] args) { String pwdGenerata = ""; PwdGenerator pg=new PwdGenerator(); for (int i=1;i<=10;i++) { pwdGenerata=pg.getPWD(); System.out.println("pwd:"+pwdGenerata); } } public PwdGenerator() { } public static String getPWD() { Random rnd = new Random(); //decido dimensione password int dim = rnd.nextInt(5); dim = dim+6; //decido la quantità di cifre numeriche int num = rnd.nextInt(dim-4)+1; num= num+2; //decido la quantità di cifre alfa int alfa = dim-num; int appoggio=0; String pass=""; int alterna=0; int contnum=1; int contalfa=1; for (int i=1;i<=dim;i++) { alterna = rnd.nextInt(2); if (contalfa>alfa){alterna=0;} else{if (contnum>num) {alterna=1;} } if (alterna==1) {contalfa++; appoggio = rnd.nextInt(25); switch (appoggio) { case 0 : pass = pass+"A";break; case 1 : pass = pass+"B";break; case 2 : pass = pass+"C";break; case 3 : pass = pass+"D";break; case 4 : pass = pass+"E";break; case 5 : pass = pass+"F";break; case 6 : pass = pass+"G";break; case 7 : pass = pass+"H";break; case 8 : pass = pass+"I";break; case 9 : pass = pass+"J";break; case 10 : pass = pass+"K";break; case 11 : pass = pass+"L";break; case 12 : pass = pass+"M";break; case 13 : pass = pass+"N";break; case 14 : pass = pass+"O";break; case 15 : pass = pass+"P";break; case 16 : pass = pass+"Q";break; case 17 : pass = pass+"R";break; case 18 : pass = pass+"S";break; case 19 : pass = pass+"T";break; case 20 : pass = pass+"U";break; case 21 : pass = pass+"W";break; case 22 : pass = pass+"X";break; case 23 : pass = pass+"Y";break; case 24 : pass = pass+"Z";break; } } if (alterna==0) {contnum++; appoggio = rnd.nextInt(10); switch (appoggio) { case 0 : pass = pass+"0";break; case 1 : pass = pass+"1";break; case 2 : pass = pass+"2";break; case 3 : pass = pass+"3";break; case 4 : pass = pass+"4";break; case 5 : pass = pass+"5";break; case 6 : pass = pass+"6";break; case 7 : pass = pass+"7";break; case 8 : pass = pass+"8";break; case 9 : pass = pass+"9";break; } } } return pass; } }

Rispondi quotando
..