lundi 2 décembre 2019

Unable to set a random enum to a field

public class Player {

public String name;
public boolean active;
public boolean seeker;
public Location randLocation;

public Player(String name, boolean seeker) {
    this.name = name;
    this.seeker = seeker;
    //this.active= true;
    //this.randLocation=randLocation;
}

public String getName() {
    return name;
}

public boolean isActive() {
    return active;
}

public void setActive(boolean active) {
    this.active = active;
}

public boolean isSeeker() {
    return seeker;
}

public Location getrandLocation() {
    Random random = new Random();
    randLocation = (Location.values()[random.nextInt(Location.values().length)]);
    System.out.println(randLocation);
    return randLocation;
}

public void setRandLocation(Location randLocation) {
    this.randLocation = randLocation;

@Override
public String toString() {
    return "Player [name=" + name + ", active=" + active + ", seeker=" + seeker + ", randLocation=" + randLocation
            + "]";
}

public static void main(String[] args) {
    Player p1 = new Player("p1", false);
    System.out.println(p1);
}```

where Location is an enumerated set of locations like {BEDROOM, GARDEN, KITCHEN}.etc. When I create a player, and then print out the player, randLocation is always null, i've tried altering the constructer but still no avail. Any help would be useful! Thank you.




Aucun commentaire:

Enregistrer un commentaire