dimanche 3 avril 2016

Filling an array with random numbers in a for-loop [duplicate]

This question already has an answer here:

I am trying to write a program in Java that asks the user how many random numbers to generate (0-999) Then the program is supposed to fill an array up with the random numbers and present them to the user.

This is what I got so far:

import java.util.*;

public class random2
{
public static void main ( String[] args )
{
    Random random;
    int i;
    int[] numbers;
    int numberOfNumbers=0;
    int upperRange=999;
    int lowerRange=0;
    int randomNumber=0;

    Scanner in = new Scanner(System.in);

    System.out.println("How many random numbers between 0-999?");
    numberOfNumbers=in.nextInt();
    numbers = new int [numberOfNumbers];


    random = new Random();

    for (i = 0; i < numberOfNumbers; i++){

    randomNumber = random.nextInt(upperRange-lowerRange) + lowerRange;
    numbers[i] = randomNumber;

    if (i==numberOfNumbers) 

    System.out.println(randomNumber);


    }      


  }

}

But the program won't present the list of the random numbers. It just exits. What did I do wrong?




Aucun commentaire:

Enregistrer un commentaire