Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组元素不工作的算术公式_Java_Math_For Loop_Methods_Random - Fatal编程技术网

Java 数组元素不工作的算术公式

Java 数组元素不工作的算术公式,java,math,for-loop,methods,random,Java,Math,For Loop,Methods,Random,这是一个使用Buffons针模拟来估计pi值的程序。 我在这里遇到的问题是,在方法calcPi中,pi数组的元素在for循环中的算术公式之后一直显示为0。我查过了,计数器和审判台都在工作。。帮忙 import java.util.Scanner; public class Darts { public static int trials() //User Inputs number of trials { int input; Scanner in

这是一个使用Buffons针模拟来估计pi值的程序。 我在这里遇到的问题是,在方法calcPi中,pi数组的元素在for循环中的算术公式之后一直显示为0。我查过了,计数器和审判台都在工作。。帮忙

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);
    }
}
import java.util.Scanner;
公共级飞镖
{
public static int trials()//用户输入试用次数
{
int输入;
扫描仪输入=新扫描仪(系统输入);
系统输出打印(“多少省道/试验?”);
input=in.nextInt();
返回输入;
}
public static double[]randomX(int-trial)//根据试验量随机生成x坐标的数字
{
double[]RandNumarayx=新的double[试用版];
for(int i=0;i如果(Math.pow(randx[i],2)+Math.pow(randy[i],2)你的问题可能是这一行:

pi[i]=4*(命中计数器/(i+1));

您正在进行整数除法,由于
hitcounter
始终低于
i
它将始终返回0

试试这个

pi[i]=4*(命中计数器/(i+1d));


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

这可能是由于整数除法

试一试

pi[i]=4*(命中计数器/(i+1.0));

Java中的
1.0
literal是double类型。基本上,从那里开始
此表达式中的所有其他值也将提升为双精度