mardi 22 mars 2016

Sorting an array of 12 randomly generated values

I am trying to sort an array of randomly generated values but I think I may have this arrays.sort in the wrong place or it does not have the correct syntax.

Could someone explain to me what I need to do to get it to work properly?

Here's the code.

package name;

import java.util.*;

public class name {

    public static void main(String[] args) {

        int years = isValidInput();

        Random generator = new Random();
        float[] monthsArray = new float[12];

        for (int i = 1; i <= years; i++){
            System.out.println("\nYear " + i + ": ");
            for (int months = 1; months <= 12; months++){
                System.out.println("Month " + months + ": " + generator.nextFloat() * 100);
                Arrays.sort(monthsArray);
            }
        }
}

    public static int isValidInput(){

        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the desired number of years: ");
        int years = keyboard.nextInt();

        while (years <= 1 || years >= 80){
            System.out.print("Please enter a number that is greater than 1 and less than 80: ");
            years = keyboard.nextInt();
            System.out.println();
        }
        return years;
        }

    }




Aucun commentaire:

Enregistrer un commentaire