我的程序有点问题,我应该通过在半径为 1 的圆形板上投掷飞镖的一系列试验来估计 pi。我遇到麻烦的地方是当我尝试编译它时出现错误“找不到符号 - 变量 X"。我真的不知道为什么会这样,我知道当事情拼写错误时会发生这个错误,但我知道我没有拼错 X。如果你能提供帮助,我将不胜感激!
/**
* The purpose of this program is to calculate pi by throwing darts
*
*
*
*/
import java.util.Scanner;
public class Darts
{
//create an array length number of darts for x and y
//method to generate random numbers
//calc pi = 4 hits / attempts
//method to print
public static int dartHit (double X, double Y, int hit)
{
X = Math.random();
Y = Math.random();
hit = 0;
if ( Math.pow(X, 2) + Math.pow(Y, 2) <= 1)
{
hit++;
}
return hit;
}
public static double calcPi (int hit, int attempts, double pi)
{
pi = 4 * ((hit * 1.00) / (attempts * 1.00));
return pi;
}
public static void main(String[ ] args)
{
Scanner in = new Scanner(System.in);
System.out.println("How many darts per trial? ");
int attempts = in.nextInt(); //(N)
System.out.println("How many trials?");
int trials = in.nextInt();
double [] pies = new double [trials];
double sum = 0.0;
for (int j = 0; j < trials; j++)
{
for ( int i = 0; i < attempts; i++)
{
int dartHit = dartHit (X, Y, hit);
double calcPi = calcPi(hit, attempts, pi);
sum += calcPi;
}
double average = sum / attempts;
pies [j] = average;
}
for (int k = 0; k < pies.length; k++)
{
System.out.printf( "Trial [" + k +"]: pi = " + "%8.6f \n", pies [k]);
}
}
}