0

I am working on making a text based game in Java, I am using a class and instance variables and I am trying to create a method that generates a random number based on the number that the instance variable max_attack has.

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

Output

troll1005089

I would like the method to create random numbers between 1 and 100.

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
Demiah
  • 41
  • 8
  • By convention, class names start with an upper case letter. Consider naming your classes as `BadPlayer` and `Troll`. – Koray Tugay Mar 19 '20 at 00:49

2 Answers2

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

Java Generate Random Number Between Two Given Values

Jocke
  • 2,189
  • 1
  • 16
  • 24
0
int result = 1 + random.nextInt(100);

result - random number from 1 to 100 https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt--

It Grunt
  • 3,300
  • 3
  • 21
  • 35
Destroyer
  • 785
  • 8
  • 20