In this program (Java), I try to make a roll of dice. How do I do it so it rolls a lot of times and adds the reels?

advertisements
import java.util.Random;

public class dice
{
  private int times;
  private int roll;
  private int side;
  Random roller = new Random();

  public void setTimes(int sides)
  {
    times = sides;
  }

  public void setSides(int die)
  {
    side = die;
  }

  public int getRoll() //this is where the "rolling" happens
  {
    int total = 0;
    int c = 0;
    while (c <= times)
    {
      c = c + 1;
      int rol = 0;
      roll = roller.nextInt(side) + 1;
      rol = rol + roll;
      total = rol;
    }
    return total;
  }
}

If you need the GUIWindow and the main, just ask


Well, rather than completely solving it for you and giving you the code... you've got a total variable, which would suggest it's the total of several values. But you're only ever directly assigning a value to it. Have you considered adding the current roll to total?