jeudi 31 mai 2018

Returning forloop to if statement

I'm making a IntegerList in Java. The list containt 6 Integers. [0,1,2,3,4,5]. I have a function getNormalList(). This list must return a list with 3 random Integers. And it may not be 3 the same Integers. The function getNormalList() is not working like I want it to work.

public class SpinResultGenerator {

    public ArrayList<Integer> getNormalList() {
        ArrayList<Integer> integerList = new ArrayList<Integer>();
        Random r = new Random();
        int Low = 0;
        int High = 6;
        for (int i = 0; i < 3; i++) {
            int number = r.nextInt(High - Low) + Low;
            integerList.add(number);
        }
        if (integerList.get(0) == integerList.get(1) && integerList.get(0) == integerList.get(2)
                && integerList.get(1) == integerList.get(2)) {
            integerList.clear();
            for (int i = 0; i < 3; i++) {
                int number = r.nextInt(High - Low) + Low;
                integerList.add(number);
            }
        }
        return integerList;
    }

    public ArrayList<Integer> getJackpotList() {
        ArrayList<Integer> integerList = new ArrayList<Integer>();
        integerList.add(5);
        integerList.add(5);
        integerList.add(5);
        return integerList;
    }
}

In this way, if the result = for example: [4,4,4]. The forloop then work again and is still able to output 3 the same Integers.




Aucun commentaire:

Enregistrer un commentaire