0

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...

asked Dec 11, 2017 at 2:17
1
  • Can you show what you are trying to do? Commented Dec 11, 2017 at 21:16

1 Answer 1

1

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.

answered Dec 11, 2017 at 3:11
Sign up to request clarification or add additional context in comments.

6 Comments

Seems that @Prodian has done exactly what you asked for. You can just split the loops into two and do as required if you need to have a separate loop to get calculations.
for some reason the top 'NumberFormat' is showing an error along with DecimalFormat and ArrayList. @clinomaniac
@clinomaniac error: cannot find symbol NumberFormat formatter = new DecimalFormat("#0.00"); symbol: class NumberFormat location: class DiceRoller //Same for the ArrayList
You need to import the proper libraries. Add these before your class name and after your package declaration. import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.ArrayList; import java.util.Scanner;
I was confused about what I was reading at first. Prodian says that I would be able to get this done with 2D Array? Are you familiar with that? It would help me learn a bit more, since I feel like ArrayList is a bit ahead of me.@clinomaniac
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.