简体   繁体   中英

How to Generate Random Numbers using ENUM in Java

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);
     }
   }

You can try to use the Random class of Java, i let you some example:

Random r = new Random();
int number = r.nextInt(10)+1; 
System.out.println(number); 

The reason that this doesn't work is that your if statements are testing whether the array that enumerates all RandomNumbers instances is equal to some String . Of course, it never could be. When you call randN.values() , you are actually invoking the static method RandomNumbers.values() , the same method you used in your getRandom() method. If you want a string that you can compare, randN.name() would work, but it's really not a good idea.

If you insist on comparing something, you should use an identity comparison, like this:

int num; /* Notice I'm not prematurely assigning meaningless garbage here. */
if (randN == RandomNumbers.ONE)
  num = 1;
else if(randN == RandomNumbers.TWO)
  num = 2;
else if(randN == RandomNumbers.THREE)
  num = 3;
else
  throw new IllegalArgumentException("Unrecognized RandomNumber: " + randN);

The next step forward would be to use a switch statement:

int num;
switch(randN) {
  case ONE: num = 1; break;
  case TWO: num = 2; break;
  case THREE: num = 3; break;
  default: throw new IllegalArgumentException();
}

You aren't making good use of an enum. Here's an approach that takes full advantage of an enum type:

public enum RandomNumber {

  ONE(1),
  TWO(2),
  THREE(3);

  private final int value;

  private RandomNumber(int value)
  {
    this.value = value;
  }

  public int getValue()
  {
    return value;
  }

  public static RandomNumber roll()
  {
    RandomNumber[] values = values();
    int idx = ThreadLocalRandom.current().nextInt(values.length);
    return values[idx];
  }

  public static void main(String... argv)
  {
    RandomNumber num = RandomNumber.roll();
    System.out.printf("The random number is %s (%d).%n", num, num.getValue());
  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM