Reputation: 161
run:
How many dice do you want to roll: 3
How many sides on die number 1: 5
How many sides on die number 2: 4
How many sides on die number 3: 6
How many times do you want to roll: 65
Results
[3] 1 0.0%
[4] 2 0.0%
[5] 4 0.0%
[6] 6 0.0%
[7] 4 0.0%
[8] 12 0.0%
[9] 12 0.0%
[10] 8 0.0%
[11] 12 0.0%
[12] 0 0.0%
[13] 2 0.0%
[14] 2 0.0%
BUILD SUCCESSFUL (total time: 7 seconds)
I'm trying to figure out how to calculate the percent of the 2nd column to the third.
Here is what I have, but I know I need to do something else.
I would rather stray from using that hashmap and just use a more correct problem.
for (int i = minRoll; i < maxRoll; i++) {
int dicer = sumArray[i];
double percent = dicer/numRol;
System.out.printf("[%d] \t %d \t %.1f%% \n", i , sumArray[i], percent);
}
Ninja Edit: Here is the rest of my code
import java.util.Scanner;
/**
*
* @author joe
*/
public class DiceSimulator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many dice do you want to roll: ");
int amountOfDie = input.nextInt();
System.out.println("");
//declare diceArray
Die[] diceArray = new Die[amountOfDie];
//create a new Die for each reference
int maxRoll = 0;
for (int i = 0; i < diceArray.length; i++) {
System.out.print("How many sides on die number " + (i + 1) + ""
+ ": ");
int numSides = input.nextInt();
diceArray[i] = new Die(numSides);
int minRoll = amountOfDie;
maxRoll += numSides;
}
int minRoll = amountOfDie;
// int[] sumArray = new int[maxRoll + 1];//to store sum
System.out.println("");
System.out.print("How many times do you want to roll: ");
int numRol = input.nextInt();
System.out.println("\nResults");
int[] sumArray = new int[maxRoll + 1];
for (int i = 0; i < numRol; i++) {
int sum = 0;
for (Die d : diceArray) {
int roll = d.roll();
sum += roll;
}
sumArray[sum]++;
}
System.out.println("");
for (int i = minRoll; i < maxRoll; i++) {
int dicer = sumArray[i];
double percent = (dicer/numRol)*100;
System.out.printf("[%d] \t %d \t %.1f%% \n", i , sumArray[i], percent);
}
}
}
Upvotes: 0
Views: 1094
Reputation: 46239
You are using integer arithmetics, which means that your result gets converted to an int
before being saved in the percent
variable.
To avoid this, just cast one of the variables like this:
double percent = (double)dicer / numRol;
As @PaulHicks says, you really should multiply by 100
. You can do it like this, by declaring it as a floating point literal (100.0
), to avoid casting altogether:
double percent = 100.0 * dicer / numRol;
Upvotes: 3
Reputation: 14009
Alternatively, you could increase accuracy by staying away from floating point maths:
int percentX10 = (1000 * dicer) / numRol;
String percentString = String.format("%d.%d", percentX10 / 10, percentX10 % 10);
Upvotes: 0
Reputation: 643
dicer/numRol will always return 0 since dicer and numRol are integers, and numRol > dicer!
To get a percentage result, you have to change the type of dicer to double instead of int
replace:
int dicer = sumArray[i];
with:
double dicer = sumArray[i];
Upvotes: 0
Reputation: 1043
Change this
double percent = dicer/numRol;
to
double percent = ((double)dicer/numRol)*100;
Upvotes: 0