1 import java.util.Scanner;
2
3 public class Dice
4 {
5 public static void main(String[] args)
6 {
7 int[] counters = countInputs(6);
8 printCounters(counters);
9 }
10
11 /**
12 Reads a sequence of die toss values between 1 and sides (inclusive)
13 and counts how frequently each of them occurs.
14 @return an array whose ith element contains the number of
15 times the value i occurred in the input. The 0 element is unused.
16 */
17 public static int[] countInputs(int sides)
18 {
19 int[] counters = new int[sides + 1];
20
21 System.out.println("Please enter values, Q to quit:");
22 Scanner in = new Scanner(System.in);
23 while (in.hasNextInt())
24 {
25 int value = in.nextInt();
26 if (1 <= value && value <= sides)
27 {
28 counters[value]++;
29 }
30 else
31 {
32 System.out.println(value + " is not a valid input.");
33 }
34 }
35 return counters;
36 }
37
38 /**
39 Prints a table of die value counters.
40 @param counters an array of counters.
41 counters[0] is not printed.
42 */
43 public static void printCounters(int[] counters)
44 {
45 for (int j = 1; j < counters.length; j++)
46 {
47 System.out.println(j + ": " + counters[j]);
48 }
49 }
50 }
51