mardi 12 février 2019

Why do I get an error when using Random in Java?

I'm trying to change what image I have, depending on the value of a random integer, however, eclipse says that the rand integer needs a body.

I've already tried doing

int rand = random.nextInt(4);

but that also doesn't work. Any help would be greatly appreciated.

public class GrassTile extends Tile {
    Random random = new Random();
    static BufferedImage texture;
    int rand;

    rand = random.nextInt(4);

    if (rand == 0) {
        texture = Assets.grass0;
    } else if(rand == 1) {
        texture = Assets.grass1;
    } else if(rand == 2) {
        texture = Assets.grass2;
    } else if(rand == 3) {
        texture = Assets.grass3;
    }

    public GrassTile(int id) {
        super(texture, id);
    }
}

Thanks, JavaDev




Aucun commentaire:

Enregistrer un commentaire