so i need to generate 10 random integers in the range 1-20 but i have to store them in an array called numbers. Then I have to call a method called displayArray which displays the contents of the array and for the assignment i have to use a for loop to traverse the array.
The method header for the displayArray method is:
public static void displayArray(int[] array)
This is what I have done
public class RandomIntegers {
static int numbers = 0;
public static void displayArray(int[] array) {
System.out.println(numbers + "Numbers Generated");
}
}//end class
and
public class Random_Integers{
public static void main(String[] args) {
RandomIntegers[] numbers = new RandomIntegers[10];
//Generates 10 Random Numbers in the range 1 -20
for(int i = 0; i < numbers.length; i++) {
numbers[i] = (int)(Math.random() * 20);
RandomIntegers Numbers = new RandomIntegers();
numbers[i] = Numbers;
}//end for loop
for (int i = 0; i < numbers.length; i++) {
numbers Numbers = numbers[i];
Numbers[i].displayArray;
System.out.println();
}//end for loop
}//end main method
}//end class
An error appears on the lines
Type mismatch cannot convert from int to RnadomIntegers
numbers[i] = (int)(Math.random() * 20);
numbers cannot be resolved to a type
numbers Numbers = numbers[i];
Syntax error enter 'AssignmentOperator Expression' to complete expression
Numbers[i].displayArray;
I realize I need to assign an instance of the RandomIntegers class to the slot in the array to fix the first problem but i don't know how, could someone show me how do to so
and i don't know how to fix the other 2 problems i'm only learning how to use java so could someone please guide me in the right direction
You only have to use a single for loop - like this:
public static void main(String[] args)
{
int[] numbers = new int[10];
//Generates 10 Random Numbers in the range 1 -20
for(int i = 0; i < numbers.length; i++) {
numbers[i] = (int)(Math.random()*20 + 1);
}//end for loop
System.out.println("Numbers Generated: " + Arrays.toString(numbers));
}
To generate a random integer , you're best off using this:
Random rand = new Random();
numbers[i] = rand.nextInt(20)+1;
The rand.nextInt(20)
call will give a random number from 0 to 19, so adding 1 makes it from 1 to 20.
In practice, you don't need to create a new Random()
every time; you'd put this
Random rand = new Random();
at the beginning of your loop, and then
numbers[i] = rand.nextInt(20)+1;
inside it.
Since you've got several errors, I'd suggest you start again, write your code bit by bit, and check at each stage that it compiles and does what you want. For instance, start by printing a single random number out, and check that that works.
you have created an array of type RandomIntegers use this it will work int[] numbers = new int[10];
there is no problem with the code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.