Array Java Exercise
Array Java Exercise
Array Java Exercise
} // end main
Output :
Index Value
00
10
20
30
40
50
60
70
80
90
Index Value
0 2
1 4
2 6
3 8
4 10
5 12
6 14
7 16
8 18
9 20
Summing the Elements of an Array
import java.util.Random;
public class RollDie
{
public static void main( String[] args )
{
Random randomNumbers = new Random(); // random number generator
int[] frequency = new int[ 7 ]; // array of frequency counters
// roll die 6,000,000 times; use die value as frequency index
for ( int roll = 1; roll <= 6000000; roll++ )
++frequency[ 1 + randomNumbers.nextInt( 6 ) ];
System.out.printf( "%s%10s\n", "Face", "Frequency" );
// output each array element's value
for ( int face = 1; face < frequency.length; face++ )
System.out.printf( "%4d%10d\n", face, frequency[ face ]
);
} // end main
} // end class RollDie
Face Frequency
1 999690
2 999512
3 1000575
4 999815
5 999781
6 1000627
java.lang.ArrayIndexOutOfBoundsException: 14
responses[19] = 14
Rating Frequency
13
24
38
42
52
import java.util.Random;
public class DeckOfCards
{