简体   繁体   English

Java遍历对象

[英]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: 我试图遍历对象并每次更新变量,我有一个Player类,其中包含以下所有变量:

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: 您可以使用简单的for在数组列表中循环,如下所示:

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: 要获取和修改播放器的变量,可以为每个参数创建getter和setter方法,如下所示:

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: 如果您有很多相同类型的变量(s1,s11),请使用数组:

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. 如果我正确理解了这个问题,那么每个玩家都有一个名字,11个分数和一个总数。 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. 您似乎在问如何遍历玩家列表并使总数等于11个分数的总和。

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. 一种更常见的方法(使用OO语言)将只是确保总数始终等于11个分数的总和。 (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. 现在您的问题是多余的: total始终等于分数的总和,因此无需遍历播放器即可在代码中的任何位置计算总数。 You can get the total for each player with 您可以通过以下方式获得每个玩家的总数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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