I want to generate a random number between 1 and 6 and print it, but I keep getting 0 for the output. When I try putting the code in the Main class and main method only, it works, but not when I do it as I have below:
class Main {
public static void main(String[] args) {
System.out.println(Dice.randomRoll());
}
}
This is my Main class above, Dice class below.
import java.util.*;
class Dice {
private static int randomRoll;
public static void roll() {
Random r = new Random();
int low = 1;
int high = 7;
int randomRoll = r.nextInt(high-low) + low;
}
public static int randomRoll() {
return randomRoll;
}
}
>Solution :
Method roll is never called, so the value of randomRoll is never changed. Try calling roll(); first in the randomRoll method.