我正在为我的 IB 数学内部评估编写程序。
我有 Calc、Gamble、Inventory、Player 和 Test 类。我有一个字段实例变量 Players (在构造函数中实例化)和方法 playrounds ,这就是问题所在。该方法的来源如下:
ArrayList<Player> Players;
public ArrayList<ArrayList<ArrayList<Player>>> playrounds(boolean randombets, double initialmoney, double initialbet,
double betrange, int numberofrounds){
ArrayList<Player> winningPlayers = new ArrayList<>(0);
ArrayList<ArrayList<Player>> m = new ArrayList<>(0);
for(int b = 0; b < numberofrounds; b++){
if(firstround){
firstround = false;
preparePlayersfirst(randombets, initialmoney, initialbet, betrange);
winningPlayers.clear();
winningPlayers.trimToSize();
m.clear();
m.trimToSize();
}
else{
preparePlayersnext(randombets, initialbet, betrange);
}
for(int a = 0; a < playingPlayers.size(); a++){
Player u = playingPlayers.get(a);
u.rolldice();
u.checkdice();
Players.set(a, u);
playingPlayers.set(a, u);
}
Player t = getwinningPlayer();
for(int a = 0; a < playingPlayers.size(); a++){
Player u = playingPlayers.get(a);
u.setprofits(t, this);
Players.set(a, u);
playingPlayers.set(a, u);
}
distributegainslosses();
removebankruptPlayers();
winningPlayers.add(t);
m.add(Players);
for(int x = 0; x < m.size(); x++){
for(Player y: m.get(x)){
for(int z = 0; z < y.getdice().size(); z++){
System.out.print(y.getdie(z) + " ");
}
System.out.print("\t");
}
System.out.println();
}
System.out.println();
}
ArrayList<ArrayList<ArrayList<Player>>> results = new ArrayList<>(0);
ArrayList<ArrayList<Player>> t = new ArrayList<>(0);
t.add(winningPlayers);
results.add(t);
results.add(m);
return results;
}
用于打印变量 m 内的值的 for 循环给出如下输出:
2 3
6 4
6 4
5 5
5 5
5 5
6 1
6 1
6 1
6 1
预期的输出是这样的:
2 3
2 3
6 4
2 3
6 4
5 5
2 3
6 4
5 5
6 1
我知道我打印值的方式没有错。它不打印重复项。if(firstround) 和 else 与问题无关。
任何帮助将不胜感激。
-Edit-
@NPE:我尝试了您建议查看的地方的解决方案之一。它仍然不起作用。这是我所做的代码:
public ArrayList<Player> clonePlayers(){
ArrayList<Player> clone = new ArrayList<>(0);
for(Player a: Players){
clone.add(new Player(a));
}
return clone;
}
我做错什么了吗?