如何在最后一个表达式中修复未定义的运算符:trial = trial / numtrials * 4?

时间:2014-10-25 22:03:37

标签: java undefined operator-keyword

    // write static methods here
    public static double [] calcDarts(double [] trial, int numtrials)throws IOException
    {
        double x;
        double y;

        for(int n = 0; n < numtrials; n++)
        {
            x = Math.random();
            y = Math.random();

            double radius = (Math.pow(x, 2) + Math.pow(y,2));

            if(radius <= 1)
                trial = trial / numtrials * 4;                                  

        }
    }

错误说明最后一行代码:trial = trial / numtrials * 4; "The operator / is undefined for the argument type(s) double[], int."如何让它为变量试验产生双倍值?

2 个答案:

答案 0 :(得分:3)

您不能使用双数组来计算整数。这样做:你必须使用数组中的double值:

        for(int n = 0; n < numtrials; n++)
        {
            x = Math.random();
            y = Math.random();

            double radius = (Math.pow(x, 2) + Math.pow(y,2));

            if(radius <= 1)
                trial[n] = trial[n] / numtrials * 4;                                  

        }

但我觉得用参数直接计算是不好的。在Array中创建以放置结果并返回它或类似的东西。

答案 1 :(得分:0)

trial是一个双精度数组,运算符/*显然没有定义用于数组。您可能想要做的是将单个数字乘以数组,如下所示:

 trial[n] = trial[n] / numtrials * 4;