Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 同时在哈希集和数组列表中循环_Java_Loops_Arraylist_Hashset - Fatal编程技术网

Java 同时在哈希集和数组列表中循环

Java 同时在哈希集和数组列表中循环,java,loops,arraylist,hashset,Java,Loops,Arraylist,Hashset,我有一个for循环,它创建几个散列集来存储单个数据 for (int i = 0; i < numberOfPlayers; i++) { players.add(new HashSet<>()); } for(int i=0;i

我有一个for循环,它创建几个散列集来存储单个数据

for (int i = 0; i < numberOfPlayers; i++) {
        players.add(new HashSet<>());
    }
for(int i=0;i
那是给球员的

for(int i=0;i
然后,另一个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
for(哈希集播放器:播放器){
玩家=用户彩票(玩家);
}//访问单个哈希集
对于(ArrayList总计:totalScore){
存储。添加(总计);
}//访问单个哈希集
但我想一次遍历各个项目。是否可以像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
公共作废运行(整数周,整数编号)
{
国际成本;
int计数器=0;
HashSet use1=新的HashSet();
int numberOfPlayers=number;//从用户处获取
列表玩家=新阵列列表(玩家数量);
List TotalLearn=新建ArrayList();
for(int i=0;i0);
系统输出打印项次(已赚(总));
}
我正在用set创建一个彩票程序。用户应该能够输入他们希望彩票运行的周数。i、 e如果他们进入3周,计算机将每周生成一组新的随机数,以与玩家的随机数进行对比,如果他们得到了一定数量的正确数字,他们就得到了钱。用户还可以选择有多少玩家

我的主要问题是保持他们赢得的总数。由于它可以是多个玩家,我决定制作一组arraylist来存放个人分数。因此,由于每个星期的球员得分都会有所不同,所以每个arraylist都会跟踪得分。例如,如果一名玩家在第一周赢得100美元。arraylist被更新为100,如果他们赢了20美元,arraylist 20将被添加到arraylist中,从而保持一个运行总数。

据我所知,您正在为不同的任务使用这两个集合的值。因此,我建议使用两个循环来循环这两个过程

你能分享更多你的代码和你想要实现的吗

但我想在数据有限的情况下提出这一点
int numberOfPlayers=10;
ArrayList players=新的ArrayList();
//产生玩家
for(int i=0;i
以及定义为

职业玩家{
智力得分;
//…其他属性
}

玩家和金额是什么类型的集合?我更新了代码,玩家应该是一个散列集来存储单个玩家条目,而金额(我将其更改为TotalLearn)是一个数组列表,它应该保存每个人在游戏中的收入,如果每次获胜后我都不知道玩家是什么,那么更新它。设置(我,玩家)为
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