-2

I'm trying to get a program that can generate some random numbers and then find the average of those random numbers, all within set parameters.

Here's what I have so far:

import java.util.*;

public class RandomAverage {

    public static void main(String[] args) 
    {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        System.out.print("Enter n: ");
        int n = Integer.parseInt(scanner.nextLine());

        double sum =

And I know I need to end the program like this:

System.out.print("Computer generated " + n + " random numbers in range between 1 – 100 and the average " + __ + " is: ");
    }
}
xlm
  • 6,854
  • 14
  • 53
  • 55

1 Answers1

1

If you want to use random integers:

double sum = 0;
for (int i=0; i<n; i++) {
   sum += random.nextInt();
}
double average = sum/n;

Otherwise check out the Javadoc on Random for other means of generating random numbers i.e. within a range etc.

xlm
  • 6,854
  • 14
  • 53
  • 55