I am trying to create a program that allows the user to pick how many dice he wants to roll and how many sides each respective die has. At the end, the user can chose to roll the die once or 100,000 times. I'm still on the first part and can't figuire out why my currentSum method isn't working. Would love some insight please!
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("How many dice");
int diceSize = sc.nextInt();
int[] Dice = new int[diceSize];
System.out.println("Enter the number of each sides of each die");
for (int x = 0; x<Dice.length; x++){
Dice[x] = sc.nextInt();
}
DieCollection d0 = new DieCollection(Dice);
System.out.println("Dice Collection: ");
for (int x = 0; x<Dice.length; x++){
Die.setTotalSides(Dice[x]);
Die.roll(Dice[x]);
System.out.print(Die.currentSide());
System.out.print(" ");
}
System.out.println();
System.out.println(d0.currentSum());
}
}
and this is my Die class
package assignment4;
/** * * @author owner */ public class Die {
private static int currentSide;
private static int totalSides;
public Die (int sides){
this.totalSides = sides;
}
public static void setTotalSides(int side){
Die.totalSides = side;
}
public static int currentSide(){
return currentSide;
}
public static void roll (int x){
currentSide = (int)(totalSides *Math.random()) + 1;
}
}
and finally my DieCollection class that I will be using to roll many die at once. It's not finished yet so ill only show the relevant methods.
public class DieCollection {
public int [] x;
public DieCollection (int[] x){
this.x = new int[x.length];
for (int i = 0; i < x.length; i++) {
this.x[i] = x[i];
}
}
public int currentSum(){
int sum = 0;
for (int y = 0; y<(x.length); y++){
Die.setTotalSides(x[y]);
Die.roll(x[y]);
sum = Die.currentSide() + sum;
}
return sum;
}