I'm working on this homework assignment which asks to write a method called createRandomIntArray that will create and return the address of an array of size n (that you create) of random integers between low and high, where low, high, and n are passed to your method. The array should be comprised of numbers between low and high and with a length of n. For this question I want the length to be random but I'm trying to get an array to be in a range of low and high numbers. I'm having trouble figuring out how to do that. Are there any suggestions on what I'm doing wrong and how to fix it?
import java.util.Arrays;
import java.util.Random;
public class Q1 {
public static void main(String[] args) {
Random random = new Random();
int x = random.nextInt(11);
int[] y = createRandomIntArray(0, 10, x);
System.out.println(Arrays.toString(y));
}
public static int[] createRandomIntArray(int low, int high, int n) {
int newLow = 0;
int newHigh = 100;
int[] result = new int[n];
for (int i = 0; i < result.length; i++)
if (result[i] < newLow)
newLow = result[i];
else if (result[i] > newLow)
newHigh = result[i];
return result;
}
}
Aucun commentaire:
Enregistrer un commentaire