简体   繁体   中英

Calling a method and returning to an array

I'm working on a program and it involves arrays. I'm new to this so I keep writing code and deleting it over and over.

I need to ask the user to input a number from 1 to 99.

Their input should allocate the size of a single dim array.

Then I have to send that array to a method that randomly chooses DOUBLES from 55 to 100 and place them into the array.

This is what I'm playing around with now. I'm starting to confuse myself!

public static void main(String[] args) {

    int scores = 0;
    int numberOfGrades;

    Scanner input = new Scanner(System.in);


    System.out.println("Please enter a number of grades from 1 to 99: ");
    numberOfGrades = input.nextInt();

    int[] amountOfGrades = new int[numberOfGrades];

    randomGrades(amountOfGrades);

    System.out.print("The letter grades for each score are: ");

} //End Method


   /**
    * 
    * @param array
    * @return
    */
    public static int randomGrades(int[] array) {
        for (int cntr = 55; cntr < 100; cntr++) {
            int rndm = new Random().nextInt(array.length);
            return array[rndm];

        }//End Main Method

Try the following code:

package eu.webfarmr;

import java.util.Random;

public class DoubleArray {
    public static void main(String[] args) {
        int numberOfGrades = 10;
        int[] grades = generateRandomGrades(numberOfGrades);
        for (int grade : grades){
            System.out.println(grade);
        }
    }

    private static int[] generateRandomGrades(int numberOfGrades) {
        int[] grades = new int[numberOfGrades];
        for (int i = 0; i < numberOfGrades; i++){
            grades[i] = new Random().nextInt(45)+55;
        }
        return grades;
    }
}

It generates an array of size 10 and stores a new random value between 55 inclusive and 100 exclusive inside each element of the array.

Solution:

public static double[] randomGrades(double[] array) {
    Random rndm = new Random(); // creating Random outside of the for loop, not in each iteration in for-loop to save memory
    for (int i = 0; i < array.length; i++) { // for each element of an array
        array[i] = (double) (rndm.nextInt(45) + rndm.nextDouble() + 55);
    }
    return array; // return array of doubles
}

Also change the following line in the main() method, from:

int[] amountOfGrades = new int[numberOfGrades];

to:

double[] amountOfGrades = new double[numberOfGrades];

as you said in question that you want to store double valuees instead of int s.

Explanation of the for-loop:

array[i] = (double) (rndm.nextInt(45) + rndm.nextDouble() + 55);

rndm.nextInt(45) returns int from 0(inclusive) to 45(exclusive),

rndm.nextDouble() returns double from 0.0(inclusive) to 1.0(exclusive)

Min possible value: 0 + 0.0 + 55 = 55

Max possible value: 44 + 1.0 + 55 = 100 (note that random double is not exactly 1.0 as it's exclusive, but it's any other possible double value close to 1.0 due to huge double precision


Edit:

With line randomGrades(amountOfGrades); , you create random double values and store them in an array, but you neither print these values to the console nor assign these values to an array: You might want to consider changing this line:

randomGrades(amountOfGrades);

to:

amountOfGrades = randomGrades(amountOfGrades); // that way you have access to result doubke values later
System.out.println(Arrays.toString(amountOfGrades));

Whole working program:

Ok, I include whole solution for your convenience, but try not to copy it blindly, try to understand what's happening here (if you need to pass int value to randomGrades() instead of double[] as argument, uncomment all comments that I included and delete parts that are not necessary):

import java.util.*;

public class Test {

    public static void main(String[] args) {
        int scores = 0;
        int numberOfGrades;

        Scanner input = new Scanner(System.in);

        System.out.println("Enter amount:");
        numberOfGrades = input.nextInt();

        double[] amountOfGrades = new double[numberOfGrades];
        amountOfGrades = randomGrades(amountOfGrades /* or: amountOfGrades.length */);
        System.out.println(Arrays.toString(amountOfGrades));

        System.out.println("Letter grades...");

    }

    public static double[] randomGrades(double[] array /* or: int amount */) {
        Random rndm = new Random();
        // add this line if you use int argument: double[] array = new double[amount];
        for(int i = 0; i < array.length; i++) {
            array[i] = (double)(rndm.nextInt(45) + rndm.nextDouble() + 55);
        }
        return array;
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM