【问题标题】:Arithmetic equation for array elements not working数组元素的算术方程不起作用
【发布时间】:2014-02-14 09:41:02
【问题描述】:

这是一个旨在使用 Buffons 针模拟估计 pi ​​值的程序。 我在这里遇到的问题是,在方法 calcPi 中,pi 数组的元素在 for 循环内的算术方程之后继续显示为 0。我已经检查过,hitCounter 和试用版都在工作。帮助?

import java.util.Scanner;
public class Darts
{
    public static int trials() //User Inputs number of trials
    {
        int input;
        Scanner in = new Scanner(System.in);

        System.out.print("How many darts/trials? ");
        input = in.nextInt();

        return input;
    }

    public static double [] randomX(int trial) // Randomly generates numbers for the x coordinate on the amounts of trials

    {
        double [] randNumArrayX = new double[trial];
        for(int i = 0; i < trial; i++)
        {
            randNumArrayX[i] = Math.random();
        }
        return randNumArrayX;
    }

    public static double [] randomY(int trial) // Randomly generates numbers forthe y coordinate on the amounts of trials
    {
        double [] randNumArrayY = new double[trial];
        for(int i = 0; i < trial; i++)
        {
            randNumArrayY[i] = Math.random();
        }
        return randNumArrayY;
    }

    public static int [] dartBoard(double [] randx, double [] randy) // determines whether the dark hit the board or not
    {
        int [] hitMiss = new int[randx.length];
        int [] trials = new int[randx.length];

        for(int i = 0; i < randx.length; i++)
        {
            if( Math.pow(randx[i] , 2) + Math.pow(randy[i] , 2) <= 1)
            {
                hitMiss[i] = 1;
            }
            else
            {
                hitMiss[i] = 0;
            }
        }
        return hitMiss;
    }

    public static double [] calcPi(int [] h) // Calculates pi using randomly generated numbers
    {
        int hitCounter = 0;
        double [] pi = new double[h.length];

        for(int i = 0; i < h.length; i++)
        {
            if(h[i] == 1)
            {
                hitCounter++;
            }
            pi[i] = 4*(hitCounter/(i + 1));
        }
        return pi;
    }

    public static void print(double [] pi) // prints results
    {
        for(int i = 0; i < 10; i++)
        {
            System.out.printf("%-7s%2d%-8s%-10.6f%n", "Trial [", i, "]: pi = ", pi[i]);
        }
        System.out.println("\t   .....");
        System.out.printf("%-17s%-10.6f%n", "Estimate of pi = ", pi[pi.length -1]);
    }

    public static void main(String [] args) // main method
    {
        int t = trials();
        double [] x = new double[t];
        double [] y = new double[t];
        int [] h = new int[t];
        double [] p = new double[t];
        x = randomX(t);
        y = randomY(t);
        h = dartBoard( x , y );
        p = calcPi( h );
        print(p);
    }
}

【问题讨论】:

    标签: java math for-loop methods random


    【解决方案1】:

    你的问题可能是这一行:

    pi[i] = 4*(hitCounter/(i + 1));

    你正在做一个整数除法,因为hitcounter总是低于i,所以它总是返回0。

    试试这个

    pi[i] = 4*(hitCounter/(i + 1d));

    注意1d,这将强制进行双重除法。

    【讨论】:

      【解决方案2】:

      这可能是由于整数除法。

      试试

      pi[i] = 4 * (hitCounter/(i + 1.0));

      Java 中的 1.0 字面量是 double 类型。基本上,从那里
      此表达式中的所有其他值也将提升为 double。

      【讨论】:

        猜你喜欢
        • 2015-06-02
        • 2015-10-21
        • 2018-03-24
        • 2016-08-10
        • 1970-01-01
        • 2012-06-19
        • 1970-01-01
        • 2016-05-17
        • 1970-01-01
        相关资源
        最近更新 更多