mercredi 1 juillet 2015

How do I print an array multiple times?

My following code is mostly somebody else's, I was able to figure out how to print the correct range, but now I want to print that range (array) multiple times.

    public class lottery {
    public static void main(String[] args) {

    int[] lottery = new int[6];
    int randomNum;

    for (int i = 0; i < 6; i++) {
        randomNum = (int) Math.ceil(Math.random() * 59); // Random number created here.
        for (int x = 0; x < i; x++) {
            if (lottery[x] == randomNum) // Here, code checks if same random number generated before.
            {
                randomNum = (int) Math.ceil(Math.random() * 59);// If random number is same, another number generated.
                x = -1; // restart the loop
            }

        }
        lottery[i] = randomNum;
    }

    for (int i = 0; i < lottery.length; i++)
        System.out.print(lottery[i] + " ");

    }    
}

The output, as expected is six integers in a row:

12 52 46 22 7 33

I have unfortunately not been able to find anything directly relevant to my question. I am an absolute Java beginner, so please be gentle.




Aucun commentaire:

Enregistrer un commentaire