为什么我的二维数组的元素会重复出现?
Why do elements of my 2-d array repeat itself?
int[] speed = {25, 30, 35, 40, 45};//MPH
double[] deg = {25, 30, 35, 40, 45, 50};//deg
int rows = speed.length;
int columns = deg.length;
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
trajectory[i][j] = ((Math.pow((speed[i] / 2.237), 2) * Math.sin(2 * Math.toRadians(deg[j]))) / gravity);
}
}
for(int i = 0; i < rows; i++)
{
System.out.printf("%5d ", speed[i]);
for(int j = 0; j < columns; j++)
{
System.out.printf("%9.2f", trajectory[i][j]);
}
System.out.println();
}
/* 25 9.76 11.04 11.98 12.55 12.74 12.55
30 14.06 15.89 17.25 18.07 18.35 18.07
35 19.14 21.63 23.47 24.60 24.98 24.60
40 24.99 28.25 30.66 32.13 32.63 32.13
45 31.63 35.76 38.80 40.66 41.29 40.66
*/
为什么第 6 列的输出重复第 4 列,我可以寻求帮助修复它。
代码是计算R(θ) = Vo^2 sin(2θ)/g,不同的起始角和初速度。
第 4 列和第 6 列的值可以不同,只是 Math.sin(2 * Math.toRadians(deg[j]))
会为 Math.sin(2 * Math.toRadians(40))
和 Math.sin(2 * Math.toRadians(50))
产生不同的值。
然而,
sin(2*θ) == sin(180-2*θ) == sin(2*(90-θ))
因此,θ == 40
列和 θ == 50
列的结果相同。
你的计算没有问题。如您所见here,如果 θ 为 45 度,则获得最大距离,因此对于 40 度和 50 度获得相同的距离(略小于最大距离)也就不足为奇了。
int[] speed = {25, 30, 35, 40, 45};//MPH
double[] deg = {25, 30, 35, 40, 45, 50};//deg
int rows = speed.length;
int columns = deg.length;
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
trajectory[i][j] = ((Math.pow((speed[i] / 2.237), 2) * Math.sin(2 * Math.toRadians(deg[j]))) / gravity);
}
}
for(int i = 0; i < rows; i++)
{
System.out.printf("%5d ", speed[i]);
for(int j = 0; j < columns; j++)
{
System.out.printf("%9.2f", trajectory[i][j]);
}
System.out.println();
}
/* 25 9.76 11.04 11.98 12.55 12.74 12.55
30 14.06 15.89 17.25 18.07 18.35 18.07
35 19.14 21.63 23.47 24.60 24.98 24.60
40 24.99 28.25 30.66 32.13 32.63 32.13
45 31.63 35.76 38.80 40.66 41.29 40.66
*/
为什么第 6 列的输出重复第 4 列,我可以寻求帮助修复它。
代码是计算R(θ) = Vo^2 sin(2θ)/g,不同的起始角和初速度。
第 4 列和第 6 列的值可以不同,只是 Math.sin(2 * Math.toRadians(deg[j]))
会为 Math.sin(2 * Math.toRadians(40))
和 Math.sin(2 * Math.toRadians(50))
产生不同的值。
然而,
sin(2*θ) == sin(180-2*θ) == sin(2*(90-θ))
因此,θ == 40
列和 θ == 50
列的结果相同。
你的计算没有问题。如您所见here,如果 θ 为 45 度,则获得最大距离,因此对于 40 度和 50 度获得相同的距离(略小于最大距离)也就不足为奇了。