Be seen. Boost your question’s priority for more expert views and faster solutions
import java.util.Arrays;
public class DiceHistogram {
public void getHistogram(int dices, int sides) {
int diceArray[] = new int[dices];
//Initialize Array
for (int i = 0; i< diceArray.length; i++) {
diceArray[i] = 1;
}
//System.out.println(Arrays.toString(diceArray));
for(int k=dices-2; k>=0; k--) {
for(int j=1; j<= sides; j++) {
diceArray[k] = j;
for(int i=1; i<=sides; i++ ) {
diceArray[dices-1] = i;
System.out.println(Arrays.toString(diceArray));
}
}
}
}
//for ( int j)
public static void main(String[] args) {
DiceHistogram dh = new DiceHistogram();
dh.getHistogram(2, 3);
}
}
public static List<String> dice(int nDice, int nSides) {
List<String> combinations = new ArrayList<>();
if (nDice == 1) {
// For one dice we get a list with just 1 entry
// 1
// 2
// 3
for (int side = 1; side <= nSides; side++) {
combinations.add(Integer.toString(side));
}
} else {
// When we have more than one dice we add the values for this dice
// to all possible combinations from "nDice-1"
// So add "1," to all subsequent combinations (e.g. "1,1", "1,2", "2,1", "2,2" etc.)
List<String> following = dice(nDice-1, nSides) ;
for (int side = 1; side <= nSides; side++) {
for (String follow : following) {
combinations.add(side + ", " + follow);
}
}
}
return combinations ;
}
public static void listCombinations() {
List<String> combinations = dice(4, 3) ;
for (String combo : combinations) {
System.out.println("(" + combo + ")") ;
}
}
import java.util.*;
public class Dice{
public static void main(String[] args) {
int numberOfDice = 3;
int numberOfFaces = 3;
int numberOfOutcomes = (int)Math.pow(numberOfFaces,numberOfDice);
int[] roll = new int[numberOfOutcomes]; //all initially 0 (roll to the next face)
StringBuilder[] outcomes = new StringBuilder[numberOfOutcomes];
for(int j=0; j<numberOfOutcomes; j++){
outcomes[j] = new StringBuilder("(");
}
for(int k = 1; k <= numberOfDice; k++){
int numberOnFace = 1;
for(int y = 0; y < numberOfOutcomes; y++){
if(roll[y] == 0){
outcomes[y].append(numberOnFace);
if(k != numberOfDice)outcomes[y].append(",");
numberOnFace++;
roll[y] = 1; // don't roll, just stay on the same face
if(numberOnFace == numberOfFaces + 1){
numberOnFace = 1;
roll[y] = 0; // roll to the next face
}
}else{
outcomes[y].append(numberOnFace);
if(k != numberOfDice)outcomes[y].append(",");
}
}
}
for(int z = 0; z < numberOfOutcomes; z++){
System.out.println(outcomes[z].toString() + ")");
}
}
}
I am still trying to understand. will get back to you. Thanks for your help.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Also, I assume that this is for a homework/assignment exercise, but can you please confirm if that is the case? This allows everyone to provide the correct level of assistance.