インスタンス変数で渡された乱数を生成するJavaメソッド

そう:

私は、クラスとインスタンス変数を使用していますが、私はインスタンス変数があること数に基づいて、乱数生成方法作成しようとしています、Javaでテキストベースのゲームを作るに取り組んでいmax_attackています。

public class badPlayer 
{
    String description;
    int health;
    int award;
    int max_attack;

    public badPlayer(String description, int health, int award, int attack){            
        this.description=description;
        this.health = health;
        this.award = award;
        this.max_attack = attack;
    }

   Random rnd = new Random();

    public int maxAttack(){ 
        int rand_int1 = rnd.nextInt(max_attack);
        return rand_int1;   
    }   
}

public class troll extends badPlayer
{
    troll(int attack){
        super("troll", 100, 50, 100);
    } 
}

出力

troll1005089

私は1と100の間の乱数を作成するための方法をしたいと思います。

Jocke:
Random r = new Random();
int low = 1;
int high = 100;
int result = r.nextInt(high-low) + low;

Javaは、与えられた二つの値の間の乱数を生成します

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=304201&siteId=1