Write a program that will call the rollmydie method in the

Write a program that will call the rollmydie method () in the die game class.

Your progam should prompt the user to select a number on the die 1 to 6.

Once you capture hte number enter by the user using the Scanner object. Write a method to see if the number entered is odd or even. if it is odd the die will roll 9000 times and even will roll 1000 times.Compare the frequency of each number. The highest frequency is the winner. print a message informing them if they are the winner. if it is a tie the user is the winner if they entered the number.

public class DiceGame {
  public static void main (String[] args) {
   int[] numbers = new int[6];

   }

/*********************************************************************************/
//the program rolls a dice for several times and records
//the frequency for each number in an array
//call this method

/*********************************************************************************/


  public static int[] RollMyDice (int repeat ){
   Random diceNumbers = new Random();
   int[] myIntArray = new int[6]; // defines an array with 6 elements
   int frequency1 =0; // assign zero to all the elements in the array
   int frequency2 =0;
   int frequency3 =0;
   int frequency4 =0;
   int frequency5 =0;
   int frequency6 =0;
   int face; //capture the number on the dice

  // use the for loop to roll the dice

  for(int roll = 1; roll<=repeat; roll++)
  {
   face = 1 + diceNumbers.nextInt(6); //roll the dice
   switch (face) //check the number that was rolled
   {
   case 1:
    ++ frequency1;
    break;

   case 2:
    ++ frequency2;
    break;
   case 3:
    ++ frequency3;
    break;
   case 4:
    ++ frequency4;
    break;
   case 5:
    ++ frequency5;
    break;
   case 6:
    ++ frequency6;
    break;
   }
}
  //assign the frequency to the array
  myIntArray [0] = frequency1;
  myIntArray [1] = frequency2;
  myIntArray [2] = frequency3;
  myIntArray [3] = frequency4;
  myIntArray [4] = frequency5;
  myIntArray [5] = frequency6;
  //return the array
  return myIntArray;
  }

}

Solution

DiceGame.java

import java.util.Random;
import java.util.Scanner;

public class DiceGame {
   public static void main (String[] args) {
      
       //Declaring variables
       int number,repeat,max=0,facevalue = 0,count=0;
      
       //Creating an integer array which is of size 6
   int[] numbers = new int[6];
     
   //Scanner class object is used to read the numbers entered by the user
   Scanner sc=new Scanner(System.in);
     
   //Getting the number entered by the user
   System.out.print(\"Enter a number :\");
   number=sc.nextInt();
     
   /* checking whether the number is even or odd
   * if even,set repeat to 9000
   * if odd,set repeat to 1000.
   * */
   if(number%2==0)
       repeat=1000;
   else
       repeat=9000;
     
   //calling te method RollMyDice() by passing the repeat as input
   numbers=RollMyDice(repeat);
     
   //Assigning the first element of an array to the max variable
   max=numbers[0];
     
   //Finding which number occurs maximum
   for(int i=0;i<numbers.length;i++)
   {
       /* If any otwo numbers occurs same time
       * then display message that user wins
       */
       if(numbers[i]==max)
       {
           count++;
       }
       if(count>1)
       {
           System.out.println(\":: User Wins ::\");
           System.exit(0);
       }
         
       //Finding the maximum frequency
       if(numbers[i]>max)
       {
           max=numbers[i];
       facevalue=i;
       }
   }
   //Displaying which face of the dice is the winners
   System.out.println((facevalue+1)+\" is the winner \");
   }
   /*********************************************************************************/
   //the program rolls a dice for several times and records
   //the frequency for each number in an array
   //call this method
   /*********************************************************************************/

   public static int[] RollMyDice (int repeat ){
   Random diceNumbers = new Random();
   int[] myIntArray = new int[6]; // defines an array with 6 elements
   int frequency1 =0; // assign zero to all the elements in the array
   int frequency2 =0;
   int frequency3 =0;
   int frequency4 =0;
   int frequency5 =0;
   int frequency6 =0;
   int face; //capture the number on the dice
   // use the for loop to roll the dice
   for(int roll = 1; roll<=repeat; roll++)
   {
   face = 1 + diceNumbers.nextInt(6); //roll the dice
   switch (face) //check the number that was rolled
   {
   case 1:
   ++ frequency1;
   break;
   case 2:
   ++ frequency2;
   break;
   case 3:
   ++ frequency3;
   break;
   case 4:
   ++ frequency4;
   break;
   case 5:
   ++ frequency5;
   break;
   case 6:
   ++ frequency6;
   break;
   }
   }
   //assign the frequency to the array
   myIntArray [0] = frequency1;
   myIntArray [1] = frequency2;
   myIntArray [2] = frequency3;
   myIntArray [3] = frequency4;
   myIntArray [4] = frequency5;
   myIntArray [5] = frequency6;
   //return the array
   return myIntArray;
   }
   }

_____________________________________

output:

Enter a number :6
1 is the winner

_____________________________________

Output2:

Enter a number :9
5 is the winner

____________________________Thank You

Write a program that will call the rollmydie method () in the die game class. Your progam should prompt the user to select a number on the die 1 to 6. Once you
Write a program that will call the rollmydie method () in the die game class. Your progam should prompt the user to select a number on the die 1 to 6. Once you
Write a program that will call the rollmydie method () in the die game class. Your progam should prompt the user to select a number on the die 1 to 6. Once you
Write a program that will call the rollmydie method () in the die game class. Your progam should prompt the user to select a number on the die 1 to 6. Once you

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site