J Ben
J Ben

Reputation: 129

Trying to create a Dice Roller Stats app in Java

I have written an application with a subclass that'll roll dice based on how many sides the user enters, and using an integer that'll roll the dice a certain number of times.

Such as: The user enters 6 sides, and wants to roll the dice 1000 times.

I'm also supposed to used a similar array to the one I've got coded.

What I have currently:

public class DDiceRoller {

public static void diceStats() {
    int maxNum = DiceRolling.diceSides;
    Scanner sc = new Scanner(System.in);
    int randomValue = 1 + (int) (Math.random() * maxNum);
    int randomValue2 = 1 + (int) (Math.random() * maxNum);
    int die1 = (randomValue);
    int die2 = (randomValue2);
    int sum = die1 + die2;
    int rollnum;
    int idx;
    System.out.println("Welcome to the Dice Roll Stats Calculator!");
    int[] combinations = new int[maxNum]; //I haven't even used this variable yet, don't know how.

    System.out.println("Enter amount of rolls: ");
    rollnum = sc.nextInt();

    for (idx = 0; idx < rollnum; idx++) {
    System.out.println(sum); //I know this is wrong, just don't know what to do.
    }

  }
}

The calculator will then run the program and output percentages based on how many times the program ran which results... so something like...

Output desired:

Total    Count    Percentage
-----  ---------  ----------
   2        123     3.01%    
   3        456     6.07%    
   4        etc     3.19%    
   5        ???     4.45%    
   6        ???     8.90%    
   7        ???     8.62%    
   8        ???     7.63%    
   9        ???     6.92%    
  10        ???     5.40%    
  11        ???     6.96%    
  12        ???     8.36%    

Output Currently: Right now all I'm getting is the same value typed back because all I'm doing with my 'for' loop right now is repeating 'sum', however many times the dice are rolled. The sum isn't returning a different number for each iteration either.

My main goal is to roll the dice the any number of times requested by the user. Using a Java array to store the results. For example, create a new pair of dice each time I'm about to execute the next roll. This way, I can store each pair of dice in a single array.

I'm attempting to learn how to code this in pieces, but I'm so lost right now that I feel defeated. Can't thank you enough for any guidance you can give. I truly apologize if this is confusing at all, I don't fully understand the instructions...

Upvotes: 0

Views: 358

Answers (1)

Prodian
Prodian

Reputation: 71

You can use 2D array or hash map for doing this but I will prefer 2x ArraList

NumberFormat formatter = new DecimalFormat("#0.00");  

ArrayList<Integer> numbers = new ArrayList<Integer>();
ArrayList<Integer> counts = new ArrayList<Integer>();

int maxNum = DiceRolling.diceSides;
Scanner sc = new Scanner(System.in);
int rollnum;
int randomValue;

System.out.println("Welcome to the Dice Roll Stats Calculator!");
System.out.println("Enter amount of rolls: ");
rollnum = sc.nextInt();


for (int i  = 0; i < rollnum; i++) {

    randomValue = (1 + (int) (Math.random() * maxNum)) + (1 + (int) (Math.random() * maxNum));

    if(numbers.contains(randomValue)){
        int position = numbers.indexOf(randomValue);
        counts.set(position, counts.get(position)+1);
    }else{
        numbers.add(randomValue);
        counts.add(1);
    }

}

System.out.println("Total\tCount\tPercentage");
System.out.println("-----\t---------\t----------");
for(int i = 0; i<numbers.size(); i++){
    System.out.println(numbers.get(i) +"\t" + counts.get(i) + "\t" + formatter.format(((double)(counts.get(i)*100))/rollnum) + "%";
}

is it your answer ? I hope this works.

Upvotes: 1

Related Questions