lundi 12 juin 2017

Counting how often a number in a Hashset has been picked

Here my code until now:

 public void lottoGame()
 {        
      HashSet<Integer> dups = new HashSet<Integer>();  

      for(int howMany = 1; howMany <= 6; howMany++)
      {
           int lottoNumber = lottoNum.nextInt(49) + 1;

           while(dups.contains(lottoNumber))
           {
                lottoNumber = lottoNum.nextInt(49) + 1;
           } 

           dups.add(lottoNumber);
           randomLotto[howMany] = lottoNumber;

           System.out.println(lottoNumber);

    }
    for(int counter : dups)
    {
        numberCount[counter]++; //to store the counting of random numbers
    } 

    for(int counting = 1; counting < numberCount.length; counting++)
    {
        System.out.println(counting + " occurs " + numberCount[counting] + " times");
    }
}

So basically, what I did was create a hashset and put in 6 random numbers. In my last 2 for-loops I'm trying to count how often I draw which number and print that out. The problem is, it starts with the 3rd field when printing out, for whatever reason. Does anyone know why?




Aucun commentaire:

Enregistrer un commentaire