gpt4 book ai didi

java - 使用数组执行计算

转载 作者:行者123 更新时间:2023-12-01 13:43:21 25 4
gpt4 key购买 nike

我有一个关于如何使用数组执行计算的问题。就我而言,我需要执行数组的计算,然后在 main 方法中调用它。我在返回类型中进行了计算,编译器提示 double 无法转换为 double[],所以我尝试获取数组的长度,但仍然收到相同的警告。我怎样才能解决这个问题?任何帮助将不胜感激。下面是我的代码:

// calcGravity returns an array of doubles containing teh gravity values
//and takes two arrays of doubles as parameters for the radius values and mass
public static double[] calcGravity(double[] radius, double[] mass)
{
// fill in code here
return (6.67E-17) * mass.length / Math.pow(radius.length, 2);
// The formula to calculate gravity is:
// 6.67E-17 times the massOfPlanet divided by the radius of the planet squared
}

最佳答案

public static double[] calcGravity(double[] radius, double[] mass)
{
double[] ret = new double[radius.length];
for (int i=0;i<radius.length;i++) {
// fill in code here
ret[i] = (6.67E-17) * mass[i] / Math.pow(radius[i], 2);
// The formula to calculate gravity is:
// 6.67E-17 times the massOfPlanet divided by the radius of the planet squared
}
return ret;
}

关于java - 使用数组执行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20522676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com