简体   繁体   English

如何更改在Java中使用我的2-D数组的方法

[英]How to change a method to work with my 2-D Array in Java

I have written a program using 2 separate arrays but want to combine the arrays (velocity and degrees) into one single multidimensional array called distance. 我使用2个独立的数组编写了一个程序,但希望将数组(速度和度数)组合成一个名为distance的单个多维数组。 I have never used 2-D arrays before and I am sure my code may be hard to understand. 我以前从未使用二维数组,我确信我的代码可能很难理解。

I have rewritten most of the program but I am getting held up in the method "calcDistance". 我已经重写了大部分程序但是我在方法“calcDistance”中受到了阻碍。 I tried to rewrite the method but it just turned into a big mess. 我试图改写这个方法,但它变成了一个大混乱。

double [][] distance;

Catapult(double[][] d){
   distance = d;
}

public double [][] calcDistance(){

    double [][] total = {{0,0,0,0,0,0,0} , {0,0,0,0,0,0,0}};

    for (int row = 0; row < distance.length; row++){

        for (int column = 0; column < distance.length; column++){

        total[row][column] = (Math.pow(distance[column]/2.237, 2) * Math.sin(2 * Math.toRadians(distance[column]))/9.8);

       }
    }

    return total;
}

I get a bad operand error when dividing by 2.237 and an incompatible types error when using distance[column]. 在除以2.237时出现错误操作数错误,使用距离[列]时出现不兼容类型错误。 Now my two problems are 现在我的两个问题是

1) How can I fix this method and what did I initially do wrong to cause the error 1)如何修复此方法以及我最初做错了什么导致错误

2) How can I implement both parts of my multi-d array distance in the method because as of right now it only runs through the columns of one row in "distance" 2)如何在方法中实现我的多维数组距离的两个部分,因为到目前为止它只运行“距离”中的一行的列

I really recommand you to use iterator since it make everything easier: 我真的建议你使用迭代器,因为它使一切变得更容易:

public class Catapult {
    double [][] distance;

    Catapult(double[][] d){
       distance = d;
    }

    public double [][] calcDistance(){

        double [][] total = new double[2][7];

        int i=0;

        for (double[] row : distance) {

            int j=0;

            for (double value : row) {

                total[i][j] = (Math.pow(value/2.237, 2) * Math.sin(2 * Math.toRadians(value))/9.8);

                ++j;

            }

            ++i;

        }


        return total;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM