简体   繁体   English

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

[英]Java: arithmetic with arrays (multiplication, addition, subtraction)

Below is a method I want to use to do calculations and return a value. 下面是我要用来进行计算并返回值的方法。 The equation is: L = T + R*p - b where L, R, p, and b will have a subscript (0-5). 等式为:L = T + R * p-b其中L,R,p和b将具有下标(0-5)。 R is a 3x3 matrix, which is stored/generated in another class, p and b are arrays of 6 sets of (x,y,z) coordinates. R是一个3x3矩阵,在另一个类别中存储/生成,p和b是6组(x,y,z)坐标的数组。 L should have 6 total values when the program runs, and I have chosen to store them in an array. 当程序运行时,L应该有6个总计值,而我选择将它们存储在数组中。

The first error I got was when I didn't have rotationMultiply or the for loops. 我遇到的第一个错误是当我没有rotationMultiplyfor循环时。 Instead I had rotation*platformCoords . 相反,我有rotation*platformCoords So now that I have gotten rid of that error, I get a new error "Syntax error on token "*", invalid AssignmentOperator" for the line rotation[i][j]*platformCoords[i] and another error, The operator - is undefined for the argument type(s) double, double[], for line tLength + rotationMultiply - baseCoords; 因此,现在我摆脱了那个错误,我得到了一个新的错误“令牌“ *”上的语法错误,行rotation[i][j]*platformCoords[i]无效的AssignmentOperator”,以及另一个错误,运算符-对于参数类型double,double [],对于tLength + rotationMultiply - baseCoords;行, tLength + rotationMultiply - baseCoords; .

I'm sure there will be more errors once I get those taken care of, so if you can anticipate any and give me a heads up on where I can do some more learning I'd appreciate it. 我敢肯定,一旦我解决了这些错误,就会有更多的错误,因此,如果您可以预料到任何错误,请让我提前了解在哪里可以做更多的学习,我将不胜感激。

This is one piece of a program I'm attempting to write that will help control a stewart platform. 这是我尝试编写的一个程序的一部分,它将有助于控制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[];

}

} }

This answer is by no means mathematically 100% correct. 这个答案在数学上绝不是100%正确的。 I just corrected all obvious flaws and compiletime errors. 我只是纠正了所有明显的缺陷和编译时错误。

Try the following code and build the correct answer on it: 尝试以下代码并在其上构建正确的答案:

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 })));
    }
}

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

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