0

我的程序有点问题,我应该通过在半径为 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]);
       }
   }
}
4

1 回答 1

0

问题是 main() 方法不知道变量 X、Y、hit、pi 等;在其他方法中定义。所以你会得到这些行的“找不到符号”。您可能需要阅读变量范围。

这是您的代码的重构版本:

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

        //Initialize variables
        double X = Math.random();
        double Y = Math.random();
        int hit = 0;

        double [] pies = new double [trials];
        double sum =0.0, pi = 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(dartHit, 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]);
        }
    }
}
于 2016-02-14T15:53:41.417 回答