0

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);
}
  • Maybe you could use the Java List https://docs.oracle.com/javase/8/docs/api/java/util/List.html And append all Players to the list? – Merijndk Feb 28 '18 at 14:46
  • 2
    IMHO `s1-s11` should be a single variable `s[]`, an array that contains all scores, then its easy to loop through the `s` array and calculate the scores – pragman Feb 28 '18 at 14:47
  • agree, https://stackoverflow.com/questions/18410035/ways-to-iterate-over-a-list-in-java – juju Feb 28 '18 at 14:48
  • yeah check the link above, everything you might need to know about iterating through a list. – juju Feb 28 '18 at 14:50
  • https://stackoverflow.com/questions/14489590/iterating-through-array-java – Merijndk Feb 28 '18 at 14:50
  • Your question is not very clear. You have a collection (list) of players, and each player has 11 variables (`s1`, `s2`, ..., `s11`). And for each player you want to make `total` the sum of the `s1` through `s11`? I guess the question here would be why that isn't always the case anyway, i.e. why you don't just ensure you update `total` any time you update any of the `s` variables? Or did I completely misunderstand? – James_D Feb 28 '18 at 16:09

2 Answers2

1

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.

silversoul
  • 33
  • 8
  • `players[i]` in your commented code is not consistent with `players` being a `List`... Why not just use `for (Player p : players) { ... }` (which works in either case)? – James_D Feb 28 '18 at 16:32
  • I means players.get(i), sorry. Of course you can use the for each too – silversoul Mar 01 '18 at 11:04
0

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());
}
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Ahh ok, thanks a lot how would I go about adding to the score? –  Feb 28 '18 at 16:45
  • @MorganChorlton Call `setScore(...)`? – James_D Feb 28 '18 at 16:46
  • I have tried this, but when I get (Non-static method, setScore(int, int) cannot be referenced in a static method, is there a workaround for this? –  Feb 28 '18 at 16:51
  • @MorganChorlton It's not a static method (if it was, how would it know which player's score you were setting). – James_D Feb 28 '18 at 16:55
  • that's what I was thinking but IntelliJ is displaying that error? –  Feb 28 '18 at 17:00
  • @MorganChorlton OMG. So don't try to call it as though it's a static method. – James_D Feb 28 '18 at 17:01
  • Right but my method isn't static? check the code above –  Feb 28 '18 at 17:13
  • @MorganChorlton Seriously: look at the code you posted. You are calling it with the *class name*. It **is not a static method**. – James_D Feb 28 '18 at 17:24
  • so what would be the best way to run this code, I'm new to java and not fully understanding some of the features! –  Feb 28 '18 at 19:34
  • @MorganChorlton Are you saying you don't know what a `static` method is? Otherwise it's really difficult to understand what the problem is. If you don't know what `static` means, you probably need to just go study the basics of the language; otherwise this question will just turn into an entire Java 101 tutorial, which is beyond the scope of this site. – James_D Feb 28 '18 at 19:36