【发布时间】:2021-05-05 08:31:44
【问题描述】:
我有一个 for 循环,它创建了几个 Hashset 来存储单个数据
for (int i = 0; i < numberOfPlayers; i++) {
players.add(new HashSet<>());
}
这是给玩家的
for (int i = 0; i < numberOfPlayers; i++) {
amount.add(new ArrayList<>());
} //this is the arraylist that stores the score
然后另一个for循环创建相同数量的ArrayList来存储分数
我知道如何评估我创建的单个 HashSet 和 ArrayList
for (HashSet<Integer> player : players) {
player = userLottery(player);
} // to access the individual hashSets
for (ArrayList<Integer> total : totalScore) {
storage.add(total);
} // to access the individual hashSets
但我想一次循环浏览各个项目。是否可以像 python 中那样使用高级 for 循环来做到这一点
这里是方法中的所有代码
public void run(int week, int number)
{
int cost;
int counter=0;
HashSet<Integer> use1=new HashSet();
int numberOfPlayers =number; // obtained from user
List<HashSet<Integer>> players = new ArrayList<>(numberOfPlayers);
List<ArrayList<Integer>> totalEarned= new ArrayList<>();
for (int i = 0; i < numberOfPlayers; i++) {
players.add(new HashSet<>());
}
for (int i = 0; i < numberOfPlayers; i++) {
amount.add(new ArrayList<>());
}
for (HashSet<Integer> player : players) {
player = userLottery(player);
}
System.out.println("");
do
{
week--;
counter++;
cost=+2;
HashSet <Integer>cd=new HashSet();
comp=computerLottery(comp);
System.out.println("week : "+counter);
for (HashSet<Integer> player : players) {
checkLottery(comp, player);
totalEarned.add(earned(total));
}
System.out.println("");
comp.clear();
}while(week>0);
System.out.println(earned(total));
}
I'm creating a lottery program using set. the user should be able to enter the number of weeks they would like the lottery to run. i.e if they enter 3 weeks the computer would generate a new set of random number each week to check against those of the player if they get a certain amount of the numbers right they get money. The user is also able to choose how many players
我的主要问题是保持他们赢得的总数。因为它可能不止一个玩家,所以我决定制作一堆数组列表来存放个人分数。因此,由于每周玩家的得分都会不同,因此每个数组列表都会跟踪得分。例如,如果玩家在第一周赢得 100 美元。 arraylist 更新为 100,然后如果他们赢得 $20,则将 arraylist 20 添加到 arraylist 中,从而保持运行总数。
【问题讨论】:
-
玩家和数量是什么类型的收藏?
-
我更新了代码,players 应该是一个 Hashsets 来存储单个玩家条目,而金额(我将其更改为 totalEarned)是一个数组列表,应该保存每个人的收入游戏并在每次获胜后更新它
标签: java loops arraylist hashset