I am having a slight issue when finding the minimum value of my random array. I keep getting 0 as a result for the smallest number. Largest number would appear to be fine. I also receive the wrong number for the index of both as well.
Current Code:
public class RandomArray {
public static void main(String[] args)
{
int indexHigh = 0;
int indexLow = 0;
int max = 0;
int min = 0;
int[] randArray = new int[10];
for(int i = 0; i < randArray.length; i++)
{
randArray[i] = RandomMethod.randomInt(1,1000);
System.out.print(randArray[i] + " ");
System.out.println(" ");
//Max & Min
if (randArray[i] > max)
max = randArray[i];
indexHigh = i;
if (randArray[i] < min)
min = randArray[i];
indexLow = i;
}
System.out.println("The highest minimum for the December is: " + min + " at index: " + indexLow);
System.out.println("The highest maximum for the December is: " + max + " at index: " + indexHigh);
}
}
Supporting Class(Instructions stated that it needed to be in another class. Only the method is necessary):
import java.security.SecureRandom;
import java.util.*;
public class RandomMethod {
//implement random class
static Random randomNumbers = new Random();
public static void main(String[] args)
{
//#7 -- Generates a number between 10 & 20 ( 100x )
/*
int counter = 0;
while(counter <= 100)
{
System.out.println(randomInt(10,20));
counter++;
}
*/
//# 8
//int[] randArray = new int[randomInt(1,1000)];
}
// Random Int method where value is greater than x & less than y
public static int randomInt(int x, int y)
{
int randomValue = randomNumbers.nextInt(y - x) + x;
return randomValue;
}
}
minstarts at zero and all the elements in the array are greater than1. All numbers will thus be larger thanminand yourminwill never be updated. You should set the starting value ofminto either the first element of the array, or some very large value that you know is larger than all the elements in the array.