vendredi 31 août 2018

Picking random array element

I'm trying to grab a random value from this array. When I run the program it just prints 0 for x. Why isn't it printing the updated value that is returned from the function?

import java.util.*;
public class randomArray
{
    public static void main(String[] args)
    {
        int[] myArray = new int[]{1,2,3};
        int x = 0;
        getRandom(myArray, x);
        System.out.println(x);
    }
    public static int getRandom(int[] array, int h) 
    {
        int rnd = new Random().nextInt(array.length);
        return h;   
    }
}




Aucun commentaire:

Enregistrer un commentaire