0

Here is what I have so far:

   public class Player {

    private String playerName;
    private double playerScore;

    public Player(String name){
       playerName = name;

    }

    public String getName() {
        return playerName;
    }
    public void setName(String name){
        playerName = name;
    }

    public double getScore(){
        return playerScore;
    }

    public void setScore(double name){
        playerScore = name;
    }
public static void playerArray(ArrayList<Player> arrayPlayerScores){

    for (int i = 0; i < Player.length; i++){

    }


}

I am trying to write a Java method that takes an ArrayList of Player objects (from above) and sets each of the Player score instance variables to a random value between 1 and 100

  • 5
    You've tried literally nothing. You should start by trying the approach for this if it weren't in a method... – nhgrif Dec 12 '13 at 03:04

2 Answers2

0
for(Player p: arrayPlayerScores){
    p.setScore(Math.random()*100);
}
0

You have an ArrayList (not an Array). First, I would add a class level field of type Random.

private static final Random random = new Random(
    System.currentTimeMillis()); // <-- Like this (maybe).

Second, I would use a for-each loop to iterate a List named players (not an ArrayList called arrayPlayerScores).

public static void playerArray(List<Player> players) { // <-- like this (maybe).
  for (Player p : players) { // <-- a for-each
    p.setScore(newScore);    // <-- replace newScore with an appropriate call 
                             //     to random (remember, to add 1 to get the range 1
                             //     n).
  }
}
Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249