Java:带数组的算术(乘法,加法,减法)

问题描述 投票:0回答:1

下面是我想用来进行计算并返回值的方法。等式为:L = T + R * p-b其中L,R,p和b将具有下标(0-5)。 R是3×3矩阵,其在另一类中存储/生成,p和b是6组(x,y,z)坐标的阵列。程序运行时L应该有6个总值,我选择将它们存储在一个数组中。

我得到的第一个错误是当我没有rotationMultiplyfor循环时。相反,我有rotation*platformCoords。所以现在我已经摆脱了这个错误,我得到一个新的错误“令牌上的语法错误”*,无效的AssignmentOperator“为行rotation[i][j]*platformCoords[i]和另一个错误,运算符 - 未定义参数类型double, double [],对于行tLength + rotationMultiply - baseCoords;

我确信一旦得到这些错误就会有更多的错误,所以如果你能预料到任何问题,请告诉我在哪里可以做更多的学习,我会很感激。

这是我试图编写的一个程序的一部分,它将有助于控制一个stewart平台。

public class LiCalc {

double length[];

public double legLength(double tLength, double[][] rotation, double[] platformCoords, double[] baseCoords ){

    double rotationMultiply;

      for (int i = 0; i < 3; i++){
          for (int j = 0; j < 3; j++){
              rotation[i][j]*platformCoords[i];
              return rotationMultiply;
          }
      }    

    length = tLength + rotationMultiply - baseCoords;

    return length[];

}

}

java arrays
1个回答
0
投票

这个答案绝不是数学上100%正确的。我刚刚纠正了所有明显的缺陷和编译时错误。

尝试以下代码并在其上构建正确的答案:

import java.util.Arrays;

public final class LiCalc
{

    private LiCalc()
    {

    }

    public static double[] legLength(double tLength[], double[][] rotation, double[] platformCoords, double[] baseCoords)
    {

        double[] rotatedCoords = new double[platformCoords.length];

        for (int i = 0; i < 3; i++)
        {
            double rotatedValue = 0;
            for (int j = 0; j < 3; j++)
            {
                rotatedValue += rotation[i][j] * platformCoords[i];
            }
            rotatedCoords[i] = rotatedValue;
        }

        double[] length = new double[platformCoords.length];

        for (int i = 0; i < 3; i++)
        {
            length[i] = tLength[i] + rotatedCoords[i] - baseCoords[i];
        }

        return length;

    }

    public static void main(String[] args)
    {
        System.out.println(Arrays.toString(LiCalc.legLength(new double[]{1, 0, 0},
                new double[][] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } }, new double[] { 0, 1, 1 }, new double[] { 1,
                        0, 0 })));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.