15,997,856 members
See more:
To explain about the program that I am making, it is program that asks the user how many times he would like his coin to flip. In this program, the coin of the head is even, and the odd is the tail.

I created a script that randomizes numbers from 1 to 10 based on the number you entered. And also I've made the script that how many odd and even numbers had come out, but I don't know how to make a script that shows how many times do each of the 10 random numbers occur and which number occurred most often.

Here is the script that I have made:

Java
```import java.util.*;

public class GreatCoinFlipping {

public static void main(String[] args) {
System.out.println("How many times do you want to flip the coin? : ");

Scanner sc = new Scanner(System.in);
int amount = sc.nextInt();

int[] arrNum = new int[amount];
int even = 0, odd = 0;
for (int i = 0; i < amount ; i++) {
arrNum[i] = (int)(Math.random() * 10 + 1);
System.out.println(arrNum[i]);
if (arrNum[i] % 2 == 0) even++;
else                    odd++;
}//end for
System.out.println("Head: " + even + ", Tail: " + odd);
}//end main

}//end class```

What I have tried:

What I am expecting on this script that that I want to make the script that shows how many times do each of the 10 random numbers occur and which number occurred most often and I want to make it by the count method. But the ramdon number part has to be in array method. Can someone please help me with this problem?
Posted
Updated 6-Dec-22 22:51pm

## Solution 1

You need to create an array long enough to hold all the possible numbers plus one. Each time you generate a random number then add 1 to the array item indexed by the number. Then at the end you can sort the array into order and print the results. See Java Arrays[^].

CPallini 7-Dec-22 4:41am
5.
Richard MacCutchan 7-Dec-22 4:42am
:thumbsup:
Soohyun Kang 8-Dec-22 2:24am

## Solution 2

Richard already gave you the 'clean' solution.
Since you're storing the generated random number in the `arrNum` array, there is the alternative to compute the requested values, without using addional arrays. However you have to use a couple of nested iterations:
pseudocode
```int most_often_number = 0;
int most_often_occurrences = 0;
for (n=1; n<=10; ++n)
{
int occurrences = 0;
for (i=0; i<amount; ++i)
{
if ( arrNumn[i] == n )
++occurrences:
}
// here write the occurrences of n

if ( most_often_occurrences < occurrences )
{
most_often_occurrences = occurrences;
most_often_number = n;
}
}
// here write the number having most occurrences```

Soohyun Kang 8-Dec-22 2:24am