// 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."
如何让它为变量试验产生双倍值?
答案 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;