Returning the value of an array while creating a calling method

时间:2017-04-10 00:02:51

标签: java arrays variables random callback

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.

2 个答案:

答案 0 :(得分:0)

There are few issues with your code as explained below:

(1) You did not instantiate the Random class which is causing the NullPointerException, so create the object for the Random class using new operator like Random rand = new Random()

(2) You need to declare the array before the for loop and insert random number to each element

(3) Array indexes start from 0, so you need to access the elements of the array like randomEight[0], randomEight[1], etc.. till randomEight[7], so your loop becomes for(int x = 0; x < 8; x++)

    private static Random rand = new Random();

    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();
    }

    public static int[] numbers(){
        int[] randomEight =  new int[8];
        for (int x = 0; x < 8; x++){
            randomEight[x] = rand.nextInt((100 - 50) + 1) + 50;
        }
        return randomEight;
    }

答案 1 :(得分:0)

//Declare an array to hold eight integers.
int[] randNums = new int[8];

//Use a for loop to add eight random integers, all in the range from 50 to 100, inclusive, to this array. Duplicates are okay.
Random rand = new Random(); // <-- You forgot to instantiate Random
for (int i = 0; i < randNums.length; i++) {
    randNums[i] = rand.nextInt((100 - 50) + 1) + 50; // <-- I suggest you put the random int directly to the element to simplify it and avoid more issues
}
相关问题