samedi 4 juillet 2015

Generate 3 random numbers and find the average

I am trying to generate three random integers between 0 and 50, calculates the average, and prints the result. This is what I have so far:

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package random.numbers;
import java.util.Random;
/**
 *
 * @author ericl_000
 */
public class RandomNumbers {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Random rand = new Random ();
        int RandNum = 0;

    for(int i =0; i < 3; i++)
    {
    RandNum = rand.nextInt(50);
    System.out.println(RandNum);
    }

       int average = (RandNum);
       System.out.println(average);
        }


        }

The output has the 3 random numbers, but the average is the last number of those 3 numbers. I don't know how to get the average of these 3 numbers. (I am new with Java.)




Aucun commentaire:

Enregistrer un commentaire