Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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[^].
 
Share this answer
 
Comments
CPallini 7-Dec-22 4:41am    
5.
Richard MacCutchan 7-Dec-22 4:42am    
:thumbsup:
Soohyun Kang 8-Dec-22 2:24am    
Thank you very much! Your advice was incredibly helpful to me!
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
 
Share this answer
 
Comments
Soohyun Kang 8-Dec-22 2:24am    
Thank You very much! Your advice was incredibly helpful to me!
CPallini 8-Dec-22 4:42am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900