I'm trying to create a program that will roll 2 different die, that could either be automatically 6 sided or custom sided as decided by the user. As of right now I've gotten this far and need help figuring out how to get anything to happen from the roll()
method, I've tried running it and it won't give me the random int it should.
Any ideas?
import java.util.*;
import java.lang.*;
public class RollOfTheDice
{
public static void main(String[] args)
{ Die firstDie, secondDie, face;
firstDie = new Die();
secondDie = new Die();
face = new Die();
face.getSides();
firstDie.roll();
secondDie.roll();
System.out.println("First die roll results:.");
}
}
class Die
{
int numberOfSides; //field value
public Die() //constructor
{ numberOfSides = 6;
}
public int getSides()//get method
{ Scanner inputDevice = new Scanner (System.in);
System.out.println("If looking for a custom sided die, please enter number of sides now:");
numberOfSides = inputDevice.nextInt();
return numberOfSides;
}
public void setSides(int Sides) //setmethod
{
numberOfSides = Sides;
}
public int roll()
{
//return a random-generated integer value between 1 and amount of sides
int rollResult = ((int)(Math.random() * 100)% numberOfSides + 1);
return rollResult;
}
}
Firstly, you do not actually println your roll result ever.
So to get that basic output, you can change the last 3 lines of your main() method to the following:
int firstRoll = firstDie.roll();
int secondRoll = secondDie.roll();
System.out.println("First die roll results:" + firstRoll);
System.out.println("Second die roll results:" + secondRoll);
This should at least get you to show the two dice rolls.