Program problem: Declare an array to hold eight integers. Use a for loop to add eight random integers, all in the range from 50 to 100, inclusive, to this array. Duplicates are okay. Next, pass the array to a method that sorts the array and returns another array containing only the largest and smallest elements in the original array. Print these two values in main. Then use a for each loop to display all elements of the sorted array on one line separated by a single space. This latter loop should also count the odd and even numbers in the array and determine the sum of all elements in the array.
Sample output:
The lowest element is 59
The highest element is 96
Here is the array
59 64 76 77 80 88 91 96
Evens: 5, odds: 3
Total: 631
My code:
import java.util.Random;
import java.util.Arrays;
public class X {
private static Random rand;
public static void main(String[] args){
int[] randomEight = numbers();
System.out.println("Here is the array");
System.out.println(Arrays.toString(randomEight));
System.out.println();
}
@SuppressWarnings("unused")
public static int[] numbers(){
for (int x = 1; x <= 8; x++){
//Random rand;
int randomNum = rand.nextInt((100 - 50) + 1) + 50;
int[] randomEight = {randomNum};
//(50)(Math.random() * 101);
return randomEight;
}
// int[] randomEight;
return null;
}
}
Code issues: I can figure out most of it, but I am having trouble return the value of the array in a random set of 8 integers that range between (50-100). My code keeps generating an error output. I might be overthinking the whole situation, but could someone guide me or give me an idea. The rest of it is easy to figure out and I might updated the code in the future.