mardi 10 mars 2015

Where Do I implement? Description above [on hold]



Implement a Die class so that it holds in state the number of sides and an array with a length that is the same as the number of sides on the die to hold the amount of times each time that side is rolled. Also write a roll method so that it returns a random number from 1 to the numbers of sides inclusive and keeps a running total of each time a side is rolled. Also write an accessor method for each instance variable. Write a Die Simulator class so that it asks the user how many sides are on the die and how many times to roll the die. Call the roll method that many times and then print to a console window the result of each roll, the frequency of each roll and the percent of time each side occurred.



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

public class DieArraySimulation
{
public static void main( String[] args)
{

Scanner in = new Scanner(System.in);
System.out.println("¿Cuántos rollos qué quieres?");

Random randomNumbers = new Random();
int[] array = new int[ 13 ];
int[] singleArray = new int[13];
int dice1;
int dice2;
int dice3;
int total;


for ( int roll = 1; roll <=36000; roll++ )
{
dice1 = 1 + randomNumbers.nextInt ( 6 );
dice2 = 1 + randomNumbers.nextInt ( 6 );
dice3 = 2 + randomNumbers.nextInt ( 11 );
total = dice1 + dice2;
++array[total];
++singleArray[dice3];
}

System.out.printf( "%s%16s%15s\n", "Face", "Frequency 1", "Frequency 2" );


for ( int face = 1; face < array.length; face++ )
System.out.printf( "%3d%13d%15d\n", face, array[ face ], singleArray[face] );

}

}



Aucun commentaire:

Enregistrer un commentaire