dimanche 29 novembre 2015

Java: Using a for-loop to fill in an array with random integers within a specific range [duplicate]

This question already has an answer here:

Criticism is welcome! I'm learning and wish to be corrected so that I may learn!

How would I go about using a for-loop to generate a random values to fill an array within the range of something like 1 to 50.

I have it set up to fill an array with however many numbers the user wishes for it to fill. I'm currently just playing around with arrays trying to understand how to use them in different ways. I know I'll have a test coming up where I'll have to do something similar and would like to have mastered it.

This is what I have so far... but I'd like to fill it with random integers in a range (any range), but for the sake of giving a range for learning purposes lets just go with 1-50.

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

{public static void main(String [] args)
   {
     Scanner MyScan = new Scanner(System.in);
     int Number;
     System.out.println("How many integers do you want in your array?");
     Number = MyScan.nextInt();
     int[] MyArray = new int[Number];

    }//end main
}//end class




Aucun commentaire:

Enregistrer un commentaire