Languages
[Edit]
PL

Java - pobierz losowy element z enum

6 points
Created by:
Sylwia
3500

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
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join