I am trying to generate random numbers (1 to 10) using enum. The variable "num" is not getting the updated random value, not sure what mistake I am making. Any pointer will be helpful. Thank you.
Java Code:
enum RandomNumbers
{
ONE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN;
public static RandomNumbers getRandom()
{
return values()[(int)(Math.random() * values().length)];
}
}
public class RandomNumbersEnum
{
public static void main(String[] args)
{
RandomNumbers randN = RandomNumbers.getRandom();
int num = 0;
if (randN.values().equals("ONE"))
num = 1;
else if(randN.values().equals("TWO"))
num = 2;
else if(randN.values().equals("THREE"))
num = 3;
else if(randN.values().equals("FOUR"))
num = 4;
else if(randN.values().equals("FIVE"))
num = 5;
else if(randN.values().equals("SIX"))
num = 6;
else if(randN.values().equals("SEVEN"))
num = 7;
else if(randN.values().equals("EIGHT"))
num = 8;
else if(randN.values().equals("NINE"))
num = 9;
else if(randN.values().equals("TEN"))
num = 10;
System.out.println("The random number is: " + num);
}
}
java.util.Random.nextInt(9) + 1;?