为什么在 Java 中使用 Random 时会出错?

我正在尝试根据随机整数的值更改我拥有的图像,但是,eclipse 表示 rand 整数需要一个主体。


我已经尝试过


int rand = random.nextInt(4);

但这也行不通。任何帮助将不胜感激。


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);

    }

}

谢谢,JavaDev


三国纷争
浏览 319回答 1
1回答

浮云间

您的代码不是您要初始化纹理的逻辑,它是具有非静态字段的静态字段删除纹理的静态关键字并将初始化放在构造函数中
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java