(Java) How to put random numbers into an array?
Go to solution
Solved by rockking1379,
why are you both trying to keep track of the loop position or whatever? use the dot operator. very powerful
public static void main(String[] args) { System.out.println("Starting..."); int size = 20; int[] array = new int[size]; System.out.println("Array Initialized"); Random gen = new Random(); int min = 1; int max = 100; System.out.println("Generating Randoms"); for(int i=0; i<array.length; i++) { array[i] = gen.nextInt(max - min) + min; } System.out.println("Printing Randoms Forward"); for(int i = 0; i<array.length; i++) { System.out.print(String.valueOf(array[i]) + ", "); } System.out.println("\nPrinting Randoms Backward"); for(int i=(array.length-1); i>=0; i--) { System.out.print(String.valueOf(array[i]) + ", "); } }
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now