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);

}



	   }
}

Might this work: piTotals = piTotals + pi;

Looks like you are mixing spaces and tabs, which makes the formatting look bad when posted on Daniweb. The average of anything, whether calculating PI or otherwise, is the total of all the trials divided by the number of trials. So start a "total" variable, initialize it to 0, then add each individual trial to the total, then when you are done, divide by the total by the number of trials.

In your case, it sounds like each "trial" should be each calculation of PI (each of which requires several dart throw trials within that larger PI trial). Add up all of your estimates (3.1, 3.2, 3.3, 3.2) and divide by the number of estimates (4 in this case) to get your overall estimate.

I reread your post. Sounds like you already knew this. You need to label your variables carefully (I wouldn't use pi as a name; use piEstimate). Then use a variable like overallPIEstimate. Your idea in post 2 is fine.

piTotals = 0;

for (/* for loop */)
{
   // code
   piTotals = piTotals + piEstimate;
}

overallPiEstimate = piTotals / numPIEstimates;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.