samedi 23 décembre 2017

Random character and number generator doesn't work in the way I want

        import java.util.Random;
        public class Test {
        public static void main(String[] args) {
        randomIDGenerator();
        }

 public static String randomIDGenerator() {
        Random r = new Random();

        char a = 0;
        for (int i = 0; i < 3; i++) {
             a = (char) (r.nextInt(26) + 'a');
        }
        int b = 0;
        for (int j = 0; j < 2; j++) {
            b = r.nextInt(10);
        }
        String h = "" + b;
        String n = a + h;

        System.out.println(n);
         return n;
 }
 }

I am writing a code in Java and I want my output to be 3 characters from a to z and 2 numbers from 0 to 9 (e.g. abc01) but the program gives me 1 character and 1 number (e.g. a1). Why does the program do this despite i've put 3 and 2 into loops? From all i know the first loop must operate 3 times and the second one must operate 2 times. So at the end my output have to be 3 characters and 2 numbers. Thanks!




Aucun commentaire:

Enregistrer un commentaire