lundi 7 décembre 2020

How would i print a random array so that it produces a certain random amount of values

i am trying to print a random array with a command-line argument so that it would take in a number such as 7 and print 7 random values

EDIT:

for example if i said 7

it would produce 1876379

import java.util.Random;
import java.util.Scanner;

public class exercise26 {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter array size");
    int n = input.nextInt();
    Random rd = new Random(); // creating Random object
    int[] arr = new int[1];
    for (int i = 0; i < n; i++) {
      arr[i] = rd.nextInt(); // storing random integers in an array
      System.out.println(arr[i]); // printing each array element
    }
  }
}



Aucun commentaire:

Enregistrer un commentaire