Advertisement
erikblomqvist

class: Dice

Nov 2nd, 2011
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. import java.util.Random;
  2. /**
  3.  * Write a description of class Dice here.
  4.  *
  5.  * @author (your name)
  6.  * @version (a version number or a date)
  7.  */
  8. public class Dice
  9. {
  10.     // instance variables - replace the example below with your own
  11.     private Random mDice;
  12.     private int mSides;
  13.  
  14.     /**
  15.      * Constructor for objects of class Dice
  16.      */
  17.     public Dice()
  18.     {
  19.         mDice = new Random();
  20.     }
  21.    
  22.     public Dice(int sides)
  23.     {
  24.         mSides = sides;
  25.     }
  26.  
  27.     public int rollDice()
  28.     {
  29.         if(mSides <= 0) {
  30.             // Om mSides inte har något lagrat värde eller om du anger siffran
  31.             // 0 eller ett negativt tal, så får tärningen automatiskt 6 sidor.
  32.             return mDice.nextInt(6) + 1;
  33.         }
  34.         else {
  35.             // Plocka in värdet från mSides;
  36.             return mDice.nextInt(mSides) + 1;
  37.         }
  38.     }
  39. }
  40.  
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement