0
import java.util.Random;
public class Enemy extends Character{
Random randomNums = new Random();

public Enemy (Type enemyType){
    super(enemyType);
}

public int dropCoins(){
    return randomNums.nextInt((50-30)+1)+30;
}

public static int getNumGoblins(){
    return randomNums.nextInt((5-2)+1)+2;
}
public static int getNumSkeletons(){
    return randomNums.nextInt((7-3)+1)+3;
}

}

import java.util.Random;
public class Player extends Character{
// attributes for the plauer class
// initilizes the fields
private int coins;
private Potion[] inventory;
Random randomNums = new Random();

//methods

public Player(Type playerType){
    super(playerType);
    coins = 0;
    inventory = new Potion[5];
}   

public void increaseStrength(int strengthIncrease){

}
public int getCoins(){
    return coins;
}
public void increaseCoins(int coins){

}
public void decreaseCoins(int coins){

}
public void addToInventory(String a, Type potion){

}
public void removeFromInventory(int index){

}
public void displayInventory(){

}
/*public int getNumOpenSlots(){
    return numOpenSlots;
}*/

public void battleMinion(Enemy enemy){


    Enemy goblin = new Enemy(Character.Type.GOBLIN);

    if (enemy.getName() == "Goblin"){
        for (int i = 1; i <= goblin.getNumGoblins(); i++)
        {
          System.out.printf("***%s vs %s %d***\n", getName(), enemy.getName(), i);

public abstract class Character{

public enum Type{ ROGUE, PALADIN, JACKIE_CHEN, SKELETON, GOBLIN, WIZARD}

private String name;
private int hitPoints;
private int strength;
private Weapon weapon;
//other attributes

//methods

public Character(Type characterType){
    switch(characterType){
        case ROGUE:
            //set the attributes for a Rogue
            name = "Rogue";
            // TODO: set other attributes
            hitPoints = 55;
            strength = 8;
            Weapon rogue = new Weapon("Short Sword", 1, 4);

            break;
        case PALADIN:
            //set the attributes for a Rogue
            name = "Paladin";
            // TODO: set other attributes
            hitPoints = 35;
            strength = 14;
            Weapon paladin = new Weapon("Long Sword",3,7);
            break;
        case JACKIE_CHEN:
            name = "Jackie Chen";
            hitPoints =45;
            strength = 10;
            Weapon jackie = new Weapon("Jump Kick",2, 6);
            break;
        case SKELETON:
            name = "Skeleton";
            hitPoints = 25;
            strength = 3;
            Weapon skeleton = new Weapon("Short Sword" ,1, 4);
            break;
        case GOBLIN:
            name = "Goblin";
            hitPoints = 25;
            strength = 4;
            Weapon goblin = new Weapon("Axe",2,6);
            break;
        case WIZARD:
            name = "Wizard";
            hitPoints = 40;
            strength = 8;
            Weapon wizard = new Weapon("Fire Blast", 4, 10);
            break;
    }
}

public String getName(){
    return name;
}

public int getHitPoints(){
    return hitPoints;
}
public int getStrength(){
    return strength;
}
public void setStrength(int _strength){
    this.strength =_strength;
}
public void setWeapon(Weapon _weapon){
    this.weapon = _weapon;

}

public void attack(){

}

public void increaseHitPoints(){

}
public void decreaseHitPoints(){

}

get error message

./Enemy.java:14: error: non-static variable randomNums cannot be referenced from a static context
        return randomNums.nextInt((5-2)+1)+2;
               ^
./Enemy.java:17: error: non-static variable randomNums cannot be referenced from a static context
        return randomNums.nextInt((7-3)+1)+3;
               ^

2 errors

No idea how to figure it out...

Ravi
  • 30,829
  • 42
  • 119
  • 173
michaeliop
  • 15
  • 8

2 Answers2

2

Because randomNums reference is object scoped, you can't directly access it inside the static methods, so declare it's scope as static, shown below:

static Random randomNums = new Random();

OR anyhow, you are calling getNumGoblins() and getNumSkeletons() methods using goblin (reference type of Enemy), so just remove the static keyword from the method signature like:

public int getNumGoblins(){
    return randomNums.nextInt((5-2)+1)+2;
}
public int getNumSkeletons(){
    return randomNums.nextInt((7-3)+1)+3;
}
Vasu
  • 21,832
  • 11
  • 51
  • 67
0

You are trying to refer non-static member inside static method, which is not allowed.

public static int getNumGoblins(){
    return randomNums.nextInt((5-2)+1)+2;
}
public static int getNumSkeletons(){
    return randomNums.nextInt((7-3)+1)+3;
}

You need to declare randomNums as static

static Random randomNums = new Random();
Ravi
  • 30,829
  • 42
  • 119
  • 173