Im working on something for class, and i'm stuck. I believe im pretty close, but dont know where to go. When I run my code im getting my arrays ten times, which must have something to do with my loop. Also, in my arrays, im not sure how to create the range from 20-50. Currently mine is from 1-50. Lastly, my output is not exactly what is needed. Any help would be greatly appreciated. Here is the question, followed by my code so far.
Create an array named array1 with 10 random integer numbers in the range of [20 50]. Then create an array named array2 with the same size of array1. Then copy the numbers in array1 that are greater than 35 to array2. Pad 0s in array2 if not enough numbers copied to it. For example, if array1 is {34, 23 45, 39, 28, 41, 33, 23, 42, 48}, array2 will be{45, 39, 41, 42, 48, 0,0,0,0,0}
import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;
public class Arraylab6 {
public static void main(String[] args) {
int x;
int[] array1 = new int[10];
int[] array2 = new int[10];
Random rand = new Random();
for (int i = 0; i < array1.length; i++) {
int h = rand.nextInt(50);
array1[i] = h;
}
System.out.println(Arrays.toString(array1));
for (int i = 0; i < array1.length; i++) {
if (array1[i] > 35) {
array2[i] = array1[i];
} else {
array2[i] = 0;
}
System.out.println(Arrays.toString(array1));
System.out.println(Arrays.toString(array2));
}
}
}
for-loop, not inside it.