找不到符号 - 变量。在其他方法中使用符号时的主要方法

时间:2016-01-09 14:23:34

标签: methods main-method cannot-find-symbol

此程序用于通过随机数投掷(x,y坐标)计算Pi,然后多次迭代整个过程。
我在代码的最后一行得到一个错误double average = average(pi),它说找不到符号变量pi,即使我使用它并在普通方法中声明它。

/*
@author
@version January 8th 2016
*/

import java.util.Scanner;
import java.util.Random;

import java.util.Random;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.IOException;

public class Darts2 {
    public static int numThrows;
    public static int numTrials;
    public static int misses;
    public static int hits;

    public static void prompt() {
        Scanner in = new Scanner(System.in);
        System.out.println("How many throws per trial would you like to do?: ");
        numThrows = in.nextInt();
        System.out.println("How many trials would you like to do?: ");
        numTrials = in.nextInt(); 
    }


    public static double[] randomX( int numThrows){
        int darts = 0;
        int i = 0;
        double[] cordX = new double[numThrows];
        while(darts < numThrows - 1) {
            cordX[i] = Math.random();
            i++;
            darts++;
        }
        return cordX;
    }
    public static double[]randomY(int numThrows) {
        int darts = 0;
        int i = 0;
        double [] cordY = new double[numThrows];
        while(darts < numThrows) {
            cordY[i] = Math.random();
            i++;
            darts++;
        }
        return cordY;
    }
    public static void getHits(int numThrows, double[] cordX, double[] cordY) {
        int ii = 0;
        int i = 0;
        hits = 0;
        misses = 0;
        for(i = 0; i < numThrows; i++) {
            if( Math.pow(cordX[ii],2) + Math.pow(cordY[ii],2) <= 1) {
                hits++;
            } else{
                misses++;
            }
            ii++;
        }
    }
    public static double calcPi(int misses, int hits) {
        int total = hits + misses;
        double pi = 4 * ((double)hits / total);
        return pi;
    }
    public static void print(double pi, int numThrows) {
        System.out.printf("\nThe pi estimate for this trial is: " + pi);
    }

    public double average(double[] pi) {
        double sum = 0;
        double average;
        for(int i = 0; i < pi.length; i++) {
            sum = sum + pi[i];
        }
        average = (double)sum/pi.length;
        return average;
    }

    public static void main(String[] args)throws IOException {
        prompt();
        int x = 0;
        for(x = 0; x < numTrials; x++) {
            double[] cordX = randomX(numThrows);
            double[] cordY = randomY(numThrows);
            getHits(numThrows, cordX, cordY);
            double pi = calcPi(misses, hits);
            print(pi, numThrows);
        }
        double average = average(pi);
    }
}

1 个答案:

答案 0 :(得分:1)

您在for循环中声明main方法中的 pi 变量,因此它的范围仅限于循环。

要解决此问题,请在for循环之前初始化pi变量,如下所示:

public static void main(String[] args)throws IOException
{
  prompt();
  double pi = 0;
  for(int x = 0; x < numTrials; x++)
    {
      double[] cordX = randomX(numThrows);
      double[] cordY = randomY(numThrows);
      getHits(numThrows, cordX, cordY);
      pi = calcPi(misses, hits);
      print(pi, numThrows);
    }
  double average = average(pi);
}