dimanche 4 octobre 2020

Java Random Percentage Chance

Someone can u help me to show how to make a probability / chance with percentage ?

import java.util.Random;

public class Main {

    public static void main(String[] args) {
        int a = new Random().nextInt(10);
        if (a >= 6) {
            // 60% chance
            System.out.println(a);
            System.out.println("You got a passive power");
        } else if (a >= 3) {
            // 30% chance
            System.out.println(a);
            System.out.println("You got an active power");
        } else if (a >= 1) {
            // 10% chance
            System.out.println(a);
            System.out.println("You got an ultimate power");
        } else {
            // <10% chance (maybe)
            System.out.println(a);
            System.out.println("You blessed with all powers.");
        }
    }
}

Is my program correct ?

Ty




Aucun commentaire:

Enregistrer un commentaire