I need help with a code where I have to put the number "7" at the front of the array using a random array of numbers. I use Math.random to generate the random numbers in each index. In any of these indexes, a number 7 may be generated.
import java.util.Arrays;
public class NumberShifter {
public static int[] Array(){
int[] array = new int[20];
int max = 10;
int min = 1;
int rand = 0;
int range = max - min +1;
for(int t = 0;t<=array.length-1;t++) {
rand = (int)(Math.random() * range) + min;
array[t] = rand;
}
return array;
}
}
Here's an example output for the code I have displayed (its all random numbers)
[9, 6, 3, 4, 4, 10, 7, 5, 2, 10, 3, 1, 8, 7, 4, 4, 10, 5, 9, 1]
There are two sevens in this array that have been generated randomly.
I would like to have the output where the sevens are at the front.
[7, 7, 3, 4, 4, 10, 9, 5, 2, 10, 3, 1, 8, 6, 4, 4, 10, 5, 9, 1]
How would I write the rest of my code so that I can achieve this?
P.S. I'm also in high school, so I'm sorry if I get something wrong!