简体   繁体   English

如何从两个一维数组创建一个二维数组并执行计算

[英]How to create a two dimensional array from two one dimensional arrays and perform calculations

I have a question on how to create a two dimensional array from two one dimensional arrays and perform calculations. 我有一个问题,如何从两个一维数组创建一个二维数组并执行计算。 In my case, the program that I have written has two syntax errors when I try to create a two dimensional array from the two one dimensional arrays using two for loops. 就我而言,当我尝试使用两个for循环从两个一维数组创建一个二维数组时,我编写的程序有两个语法错误。 The first for loop has a syntax error saying non-static variable angle cannot be referenced from a static context and the second for loop has a syntax error saying package velocity does not exist but I already defined it above. 第一个for循环有一个语法错误,说non-static variable angle cannot be referenced from a static context ,第二个for循环有一个语法错误package velocity does not exist但是我已经在上面定义了。 How can I fix this? 我怎样才能解决这个问题? Any help will be greatly appreciated. 任何帮助将不胜感激。 Below is my code: 下面是我的代码:

public class Catapult {

int velociy[] = {45, 67, 77, 89, 90, 56, 100};
double angle[] = {Math.toRadians(10), Math.toRadians(20), Math.toRadians(25), 
Math.toRadians(30), Math.toRadians(35), Math.toRadians(40)};

public static void calculations(int velocity[], int angle[]){
    for(int i = 0; i < velocity.length; i++){
        double answer = velocity[i] * Math.sin(angle[i]) / 9.8;
    }
}

public static void display(){
    for(int i = 0; i < angle.length; i++){
        for(int j = 0; j < velocity.length; j++){
            System.out.println(// how can I make a two dimensional array 
                    // from two one dimensional arrays?
            );
        }
        System.out.println();
    }
}
}

I notice that your array of velocities and your array of angles don't have the same length. 我注意到您的速度阵列和角度阵列的长度不同。 So I assume you are actually trying to combine every velocity with every angle in your calculation, to give a 2D result; 因此,我假设您实际上是在计算中尝试将每个速度和每个角度组合在一起,以得出2D结果。 instead of just combining them pairwise. 而不是将它们成对组合。 This is how I would do it. 这就是我要做的。

public static void main(String[] args) {
    int[] velocity = {45, 67, 77, 89, 90, 56, 100};
    double[] angle = {Math.toRadians(10), Math.toRadians(20), Math.toRadians(25), 
            Math.toRadians(30), Math.toRadians(35), Math.toRadians(40)};

    double[][] result = new double[velocity.length][angle.length];

    for (int i = 0; i < velocity.length; i++ ) {
        for (int j = 0; j < angle.length; j++ ) {
            result[i][j] = velocity[i] * Math.sin(angle[j]) / 9.8;
        }
    }

    System.out.println(Arrays.toString(result));
}

Try this: 尝试这个:

    Integer velocity[] = {45, 67, 77, 89, 90, 56, 100};
    Double angle[] = {Math.toRadians(10), Math.toRadians(20), Math.toRadians(25), Math.toRadians(30), Math.toRadians(35), Math.toRadians(40)};
    Object[][] twoDimensionalArray = {velocity, angle};

The first for loop has a syntax error saying non-static variable angle cannot be referenced from a static context 第一个for循环存在语法错误,即无法从静态上下文引用非静态可变角度

You have to define the angle-Array to be static: 您必须将angle-Array定义为静态的:

static double angle[] = {Math.toRadians(10), Math.toRadians(20), Math.toRadians(25), 
Math.toRadians(30), Math.toRadians(35), Math.toRadians(40)};

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

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