I am trying to have a randomly generated "Password" that can be verified by the fact that the 4 numbers in the sequence added together equal 21. I have already achieved my goal with a do.... while... loop. It looks like this:
int rand0;
int rand1;
int rand2;
int rand3;
do {
rand0 = (int)(Math.random() * 10);
rand1 = (int)(Math.random() * 10);
rand2 = (int)(Math.random() * 10);
rand3 = (int)(Math.random() * 10);
}
while ((rand0 + rand1 + rand2 + rand3) != 21);
System.out.println(rand0 + "" + rand1 + "" + rand2 + "" + rand3);
But I am looking for a way to achieve the same thing using a FOR loop. I am not sure if it is possible. I have made some attempts, this is my latest though it obviously failed. Any insight would be greatly appreciated:
int pass1,
pass2,
pass3,
pass4;
for ((String passWrd = pass1 + "" + pass2 + "" + pass3 + "" + pass4);
((pass1 + pass2 + pass3 + pass4) == 21);
(pass1 = (int) (Math.random() * 10)),
(pass2 = (int) (Math.random() * 10)),
(pass3 = (int) (Math.random() * 10)),
(pass4 = (int) (Math.random() * 10))) {
System.out.println(passWrd);
break;
}
Aucun commentaire:
Enregistrer un commentaire