Pergunta

First of all, Here is my output.

  run:
  How many dice do you want to roll: 8

 How many sides on die number 1: 5
 How many sides on die number 2: 3
 How many sides on die number 3: 7
 How many sides on die number 4: 8
 How many sides on die number 5: 5
 How many sides on die number 6: 3
 How many sides on die number 7: 5
 How many sides on die number 8: 6

 How many times do you want to roll: 50

 Results

 [8]     0 
 [9]     0 
 [10]    0 
 [11]    0 
 [12]    0 
 [13]    0 
 [14]    1 
 [15]    0 
 [16]    1 
 [17]    0 
 [18]    1 
 [19]    6 
 [20]    2 
 [21]    3 
 [22]    4 
 [23]    4 
 [24]    6 
 [25]    6 
 [26]    3 
 [27]    3 
 [28]    1 
 [29]    6 
 [30]    1 
 [31]    1 
 [32]    0 
 [33]    0 
 [34]    1 
 [35]    0 
 [36]    0 
 [37]    0 
 [38]    0 
 [39]    0 
 [40]    0 
 [41]    0 
  BUILD SUCCESSFUL (total time: 13 seconds)

see -----------------------------------------

I'm trying to add a 3rd column that shows the percent of the 2nd column with an actual % sign.


I've used printf as my code but I don't know if I can manipulate the second %d to become a percent

  for (int i = minRoll; i < maxRoll; i++) {

        System.out.printf("[%d] \t %d \n", i , sumArray[i]);
    }
Foi útil?

Solução

Use a hashMap to store the occurrences of each value:

// initialize
Map<Integer, Integer> occ = new HashMap<Integer, Integer>();
for (int i = 1; i <= 6; i++) {
    occ.put(i,0);
}

and then, whenever you "roll the dice, say dice = 5 you do:

occ.put(dice, occ.get(dice)+1); // update the number of occurrences of the result of the current roll 

finding the percentage now will be easy, for each "roll" it'll be:

100 * occ.get(dice) / number-of-rolls
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top