有关计算PI

时间:2015-11-22 02:03:11

标签: java

我是新成员,我的Java作业遇到了问题。基本上,我必须创建一个程序,通过在飞镖板上模拟投掷飞镖计算pi的值,我一直在修改我的代码,尝试不同的方式,并在线查找信息,但无济于事。我想在下面的代码中提供一些帮助。有什么我做错了,有没有我可以改进的代码?

以下是我必须完成作业的重要方向:

  • 在此程序中使用顶部/向下设计和程序抽象。换句话说,将功能单元放入方法中。
  • 提示用户在试验中投掷飞镖的次数以及试验次数。
  • 估算每次试验的pi。
  • 选择x和y的随机值。
  • 将满足x ^ 2 + y ^2≤1的任何(x,y)坐标计算为圆圈内的点击作为点击,将圆圈外的任何坐标计为未命中。
  • 计算所有试验的估计值的平均值并打印结果。

另请注意:我还不熟悉OOP;然而,这是我的下一个模块的主题。请不要使用我不熟悉的任何高级主题。 :)

编辑:我在一些非常有用的建议后修改了我的代码。好像我的程序现在运行正常。有什么我可以改进的吗?

import java.util.Random;
import java.util.Scanner;
public class Darts
{
    public static int prompt(int numDarts)
    {
        Scanner in = new Scanner(System.in);
        System.out.printf("%60s%n", "How many darts would you like to throw per trial? ");
        System.out.print("                                 ");
        numDarts = in.nextInt();
        System.out.println();
        return numDarts;
    }

    public static int prompt2(int numTrials)
    {
        Scanner in = new Scanner(System.in);
        System.out.printf("%51s%n", "How many trials would you like? ");
        System.out.print("                                  ");
        numTrials = in.nextInt();
        System.out.println();
        return numTrials;
    }

    public static double[] calculations(int numTrials, int numDarts, double zTotal, double totalCounter)
    {
        Random num = new Random();
        for(int i = 0; i < numTrials; i++)
        {
            int wins = 0;
            for(int l = 0; l < numDarts; l++)
            {
                double xCoordinate = num.nextDouble();
                double yCoordinate = num.nextDouble();
                zTotal = Math.pow(xCoordinate, 2) + Math.pow(yCoordinate, 2);
                if(zTotal <= 1)
                {
                    wins++;
                }
            }
            zTotal = 4 * ((double)wins / numDarts);
            totalCounter += zTotal;
            System.out.printf("%30s" + "%2d" + "%8s" + "%1.6f%n", "Trial [", i + 1, "]: pi = ", zTotal);
        }
        double[] extract = new double[2];
        extract[0] = zTotal;
        extract[1] = totalCounter;
        return extract;
        }

    public static void printResults(int numTrials, double totalCounter)
    {
        System.out.println("Estimate of PI: " + (totalCounter / numTrials));
    }

    public static void main(String[] args)
    {
        double z = 0;
        int darts = 0,
            trials = 0;
        double totalCount = 0.0;

        darts += prompt(darts);
        trials += prompt2(trials);
        double[] twoValues = calculations(trials, darts, z, totalCount);
        z = twoValues[0];
        totalCount = twoValues[1];
        printResults(trials, totalCount);
    }
}

1 个答案:

答案 0 :(得分:0)

总体而言,我认为您需要解决的最大问题是可变范围。传入calculationswinsxCoordinate之类的yCoordinate变量是没有意义的。这些应该是calculations体内声明的局部变量。

此外,您似乎将所有试验的数据混合在一起,而不是单独记录给定试验的结果。

编写一个只运行一个试验的方法而不是运行所有试验的当前方法并包含其中给定试验的所有逻辑也可能是好的。您还在内部和外部循环中重用i。这将导致不良后果。

以下是如何在单独的方法中运行单个试验的示例:

public static int runTrial(int numDarts){
    int wins = 0;
    Random random = new Random();
    for(int i = 0; i < numDarts; i++)
    {
        double xCoordinate = random.nextDouble();
        double yCoordinate = random.nextDouble();
        double zTotal = Math.pow(xCoordinate, 2) + Math.pow(yCoordinate, 2);
        if(zTotal <= 1)
        {
            wins++;
        }
    }
    return wins;
}

以下是您如何分离试验结果的示例。

public static void runAllTrials(int numTrials, int numDarts)
{
    int totalCounter = 0;

    for(int i = 0; i < numTrials; i++)
    {
        int wins = runTrial(numDarts);
        System.out.println(
                String.format("Trial [%d]: Pi = %g ",
                        (i+1),
                        calculatePi(wins, numDarts)
                )
        );

        totalCounter += wins;

    }
    System.out.println(
            String.format("Overall: Pi = %g ",
                calculatePi(totalCounter, numDarts * numTrials)
        )
    );


}

public static double calculatePi(int wins, int darts){
    return 4*((double)wins) / darts;
}

最终输出是:

How many darts would you like to throw per trial? 1000

How many trials would you like? 10

Trial [1]: Pi = 3.16400 
Trial [2]: Pi = 3.08800 
Trial [3]: Pi = 3.21200 
Trial [4]: Pi = 3.20000 
Trial [5]: Pi = 3.11200 
Trial [6]: Pi = 3.18000 
Trial [7]: Pi = 3.10000 
Trial [8]: Pi = 3.06000 
Trial [9]: Pi = 3.09200 
Trial [10]: Pi = 3.04400 
Overall: Pi = 3.12520