mercredi 23 novembre 2016

Using the same randomly generated array

I am creating an array with a size based on user input and the elements will be randomly generated (0-20). The problem I'm having is I don't know how to use the same array with the same randomly generated elements into different methods.

import java.util.Scanner;
import java.util.Random;
public class RandomArrays { 

    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        System.out.print("Enter the number of elements: ");
        int n = in.nextInt();

        array(n);
    };


    public static int[] array(int n) {
        Random r = new Random();
        int random = 0;
        int[] array = new int[n];

        System.out.print("Array: ");
        for (int i = 0; i < array.length; i++) {
            random = r.nextInt(20);
            System.out.print(a[i] + random + " ");
        }
        evenElement(a);
    return array;
    };


    public static int[] evenElement(int[] a) {
        Random r = new Random();
        int random = 0;
        for (int i = 0; i < a.length; i++) {
            random = r.nextInt(20);
            System.out.print(a[i] + random + " ");
        }
        return a;
    };


};

Yes, this is homework for me but it's keeping me from making any progress.




Aucun commentaire:

Enregistrer un commentaire