mercredi 29 novembre 2017

Add random number into ArrayList without duplication

I used the Random class to generate the integer from 1 to 20. After that, I added it into a ArrayList, but the error message showed "Cannot make a static reference to the non-static method nextInt(int) from the type Random". How should I do? Below is my code.

import java.util.ArrayList;
import java.util.Random;

public class ComputerChoose {

static ArrayList<Integer> computer_number = new ArrayList<>();

public static ArrayList<Integer> getTheNumber() {

    for(int times=0; times<5; times++)
    {
        computer_number.add(Random.nextInt(20) + 1);
    }

    return computer_number;
    }
}




Aucun commentaire:

Enregistrer un commentaire