Languages
[Edit]
PL

Java - pobierz losowy element z enum

6 points
Created by:
Sylwia
3410

1. Generuj losowe wyliczenie z klasą Random

import java.util.Random;

public class JavaEnumGetRandomElement {

    enum ColorEnum {
        GREEN,
        BLUE,
        RED,
        YELLOW;

        public static ColorEnum generateRandomColor() {
            ColorEnum[] values = ColorEnum.values();
            int length = values.length;
            int randIndex = new Random().nextInt(length);
            return values[randIndex];
        }
    }

    public static void main(String[] args) {
        System.out.println(ColorEnum.generateRandomColor()); // CZERWONY
        System.out.println(ColorEnum.generateRandomColor()); // ŻÓŁTY
        System.out.println(ColorEnum.generateRandomColor()); // ZIELONY
    }
}
Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join