Hi I'm pretty sure this is a beginner JAVA question. I ran my compiler and it showed that there is an error
Cannot make a static reference to the non-static method roll() from the type Die
on the last line of my main method.
What I am trying to do is roll two dices and add them up. My question is what is wrong with this line
and how do I fix this? Thanks in advance
/** main method
Die myDie1 = new Die();
Die myDie2 = new Die();
for(int roll=1;roll<=total;roll++)
{
counts[(Die.roll()+Die.roll())]++; //<--error here
}
**/
Dice method
public class Die
{
private final int MAX = 6; // maximum face value
private int faceValue; // current value showing on the die
//-----------------------------------------------------------------
// Constructor: Sets the initial face value.
//-----------------------------------------------------------------
public Die()
{
faceValue = 1;
}
//-----------------------------------------------------------------
// Rolls the die and returns the result.
//-----------------------------------------------------------------
public int roll()
{
faceValue = (int)(Math.random() * MAX) + 1;
return faceValue;
}
//-----------------------------------------------------------------
// Face value mutator.
//-----------------------------------------------------------------
public void setFaceValue (int value)
{
faceValue = value;
}
//-----------------------------------------------------------------
// Face value accessor.
//-----------------------------------------------------------------
public int getFaceValue()
{
return faceValue;
}
//-----------------------------------------------------------------
// Returns a string representation of this die.
//-----------------------------------------------------------------
public String toString()
{
String result = Integer.toString(faceValue);
return result;
}
}