简体   繁体   中英

Java cycling through objects

I'm trying to cycle through objects and update a variable each time, I have a Player class that has all of the following variables:

Name, s1 ,s2, s3,...., s11, total

I want to go through each variable from the start to the end and adding the score at each go for each player (all the players are in an array list).

I'm not sure what the best way to do this is, is there a way to select a specific object and then add the variable depending on who go it is.

if you need any more information please ask and thanks in advance for any help given.

public void  addScore(int turn, int score){
    Player.setScore( turn, score);
}

You can cycle in array list with a simple for, like this:

ArrayList<Player> players = ...;
for (int i = 0; i < players.size(); i++) {
    /*Operations on players[i] = the current player*/
}

To take and modify the variables of your player you can create getter and setter methods for each parameter like this:

private String name;

public String getName(){
    return name;
}

public void setName(String name){
    this.name = name;
}

If you have a lot of variables (s1, s11) of the same type, use an array:

int[] scores = new int[11];

So you can use another for cycle.

If I understand this question correctly, each player has a name, 11 scores, and a total. You seem to be asking how to iterate through a list of players and make the total equal to the sum of the 11 scores.

A more usual (in an OO language) approach would be just to ensure that the total is always equal to the sum of the 11 scores. (This is called "encapsulation", and is the fundamental idea behind all of object-oriented programming.) This is very easy to accomplish (for ease of programming, I put the scores in an array):

public class Player {

    private String name ;
    private int[] scores ;
    private int total ;

    public Player(String name) {
        this.name = name ;
        this.scores = new int[11] ; // all initialized to zero, as is total.
    }

    public void setScore(int whichScore, int score) {
        int change = score - scores[whichScore] ;
        scores[whichScore] = score ;
        total = total + change ;
    }


    public int getScore(int whichScore) {
        return scores[whichScore] ;
    }

    public int getTotal() {
        return total ;
    }

    public String getName() {
        return name ;
    }

    public void setName(String name) {
        this.name = name ;
    }
}

Now your question is redundant: the total is always equal to the sum of the scores, so there is no need to iterate through the players to compute the total anywhere in your code. You can get the total for each player with

for (Player p : listOfPlayers) {
    System.out.println("Player "+p.getName()+" has total score "+p.getTotal());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM