I have a homework assignment where I am supposed to simulate the roll of a dice using math.random() and changing it to an int. I have a single file with 2 classes and am attempting to make an object. My code compiles with the run time error "error: non-static variable this cannot be referenced from a static context." Any Idea what is happening.
I have changed the value of "value" to an integer and successfully ran the code. No other changes have come to mind yet.
public class DieTester_5AlastiCorrigan {
public static void main(String[] args){
// New object myDie.
Die myDie = new Die();
System.out.println(myDie.roll());
System.out.println(myDie.getValue());
}
// Creates a new Die Class
class Die{
private String value;
public Die( int dieRoll ) {
value = "" + dieRoll;
}
// Roll Method chooses random number between 1 - 7 and makes it an int.
public int roll() {
int max = 6;
int min = 1;
int range = max + 1;
int dieRoll = (int)Math.random()*range;
//dieRoll = (int)dieRoll;
return dieRoll;
}
// getValue Method returns final value of "value".
public String getValue() {
return value;
}
}
}
Expect the console to print out a number 1 <= x < 7 as an integer.
Error message: error: non-static variable this cannot be referenced from a static context
Die myDie = new Die();
^