samedi 2 mai 2015

Random Dice Roll

I'm kind of new to programming and I was wondering how I could write a java program for a random dice roll. The requirements for this is:

private members:
-final int numSides
^^The number of sides of the die
-public methods
^^Dice(int sides)
@@@Sets numSides to be the sides parameter.
@@@Used to create dice with varying number of sides, for example in main you could say Dice d6 = new Dice(6) to create a six-sided die.
^^int roll()
^^Returns a random integer from 1 up to and including numSides
NOTE: nextInt() from the Random class will return a value from zero up to the value passed in

So far I have

import java.util.Random;

public class Dice 
{
    private final int numSides;

    public Dice (int sides)
    {
        this.numSides = sides;

        Random dice = new Random();
        int num =0;
        int roll=0;

    }
     public int roll(int times)
     {
         int sum=0;
         for(int i=0; i<times; i++)
         {
             sum += roll();
         }

     }
     return sum;
}

I got this far from looking at some other examples, but I would like an explanation and help on how to finish it.




Aucun commentaire:

Enregistrer un commentaire