簡體   English   中英

Java:帶數組的算術(乘法,加法,減法)

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

下面是我要用來進行計算並返回值的方法。 等式為:L = T + R * p-b其中L,R,p和b將具有下標(0-5)。 R是一個3x3矩陣,在另一個類別中存儲/生成,p和b是6組(x,y,z)坐標的數組。 當程序運行時,L應該有6個總計值,而我選擇將它們存儲在數組中。

我遇到的第一個錯誤是當我沒有rotationMultiplyfor循環時。 相反,我有rotation*platformCoords 因此,現在我擺脫了那個錯誤,我得到了一個新的錯誤“令牌“ *”上的語法錯誤,行rotation[i][j]*platformCoords[i]無效的AssignmentOperator”,以及另一個錯誤,運算符-對於參數類型double,double [],對於tLength + rotationMultiply - baseCoords;行, 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[];

}

}

這個答案在數學上絕不是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 })));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM