n this program, I am calculating the value of pi by simulating throwing darts at a dartboard. I have correctly coded the formula that calculates the values of pi, however, as seen in the code, I can't figure out how to calculate the average of the pi values that I have calculated. I know I need to get the sum of all the values of pi, and divide that by the number of trials; just not sure how to implement that in code into the program. Can somebody help with this? Thanks
/**
* The purpose of this program is to calculate the value of pi by simulating throwing darts at a dart board.
*
* @author John D. Barry
* @date 02/24/09
*/
import java.util.Scanner;
public class Darts
{
//main method
public static void main (String [ ] args)
{
Scanner in = new Scanner(System.in);
System.out.println("How many times should the darts be thrown in a trial? ");
int drops = in.nextInt();
System.out.println();
System.out.println("Enter the number of trials. ");
double numberOfTrials = in.nextDouble();
System.out.println();
for (int trialNumbers = 0; trialNumbers < numberOfTrials; trialNumbers++)
{
double numberOfX = 0;
double numberOfY = 0;
double numberOfHit = 0;
double numberOfMiss = 0;
double pi = 0;
double average = 0;
while (numberOfX < drops && numberOfY < drops)
{
numberOfX++;
numberOfY++;
double xValue = Math.random() * 2 + -1;
double yValue = Math.random() * 2 + -1;
if ((Math.pow(xValue, 2)) + (Math.pow(yValue, 2)) <= 1)
{
numberOfHit++;
}
else if ((Math.pow(xValue, 2)) + (Math.pow(yValue, 2)) > 1)
{
numberOfMiss++;
}
}
pi = 4 * (numberOfHit / drops);
System.out.printf("Trial " + trialNumbers + ": pi = %12f\n", pi);
}
}
}