【问题标题】:Homework Help Regarding Calculation of PI关于 PI 计算的作业帮助
【发布时间】:2015-11-22 02:03:11
【问题描述】:

我是一名新成员,我的 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);
    }
}

【问题讨论】:

  • 听起来是个有趣的任务。你有什么问题?
  • 您究竟需要什么帮助?
  • @Coffee From "计算 pi 的值" 我假设 3.1416....
  • 我想你想为 (wins / numDarts) 使用浮点数。
  • 是的,它是 PI 的数学抽象,很抱歉造成混淆。我需要关于我得到的输出的帮助。使用上面的代码,我的输出值为 0.00。我应该得到 PI 的数值和 PI 的估计值的近似值。 @Coffee zTotal = Math.pow(xCoordinate, 2) + Math.pow(yCoordinate, 2); 等价于 z = x^2 + y^2 并分配给参数 zTotal。下一行表明如果 zTotal 的值小于或等于 1,则“wins”参数将增加 1。

标签: java


【解决方案1】:

总的来说,我认为您需要解决的最大问题是变量范围。传递给calculations 变量(如winsxCoordinateyCoordinate)是没有意义的。这些应该是在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 

【讨论】:

  • 哇,非常感谢!更改“i”增量之一完全将我的所有值更改为我应该查看的值。这和一些修补(重新排序一些代码)。
猜你喜欢
  • 1970-01-01
  • 2012-03-26
  • 2018-08-10
  • 1970-01-01
  • 2011-01-18
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多