0

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;

}

rcv40
  • 37
  • 2
  • 8
  • Well for one this is going to give you a different sum each time you call `currentSum`. There's no way to save a roll or know what a previous roll was. – markspace Mar 12 '18 at 01:21
  • What exactly is "not working" for `currentSum`? – markspace Mar 12 '18 at 01:23
  • `currentSide` should most definitely **not** be static, and none of the methods of Die should be static. – Hovercraft Full Of Eels Mar 12 '18 at 01:26
  • @HovercraftFullOfEels hmm i made it static so i could call upon those methods in the DieCollection class. – rcv40 Mar 12 '18 at 01:30
  • @markspace Basically I want it to add up the sum of the rolls, it's giving me incorrect sums each time argh – rcv40 Mar 12 '18 at 01:31
  • Because you're calling the methods wrong. You should call them on an instance, not on the class. – Hovercraft Full Of Eels Mar 12 '18 at 01:33
  • @rcv40 look here, I hope this helps. https://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods?noredirect=1&lq=1 –  Mar 12 '18 at 01:34
  • Alright so keep the instance variables static? OR should i remove all statics from the methods and instance variables. Sorry I'm a beginner and i suck at this, im tryna figuire it out...bear with me – rcv40 Mar 12 '18 at 01:59
  • There's also a much better way than doing Math.random() and casting it. You could make a random object, and use that to generate your random numbers. – Meepo Mar 12 '18 at 03:44

0 Answers0