使阵列的尺寸更灵活

时间:2018-12-24 12:22:02

标签: java

有人可以帮助我更改此代码,我试图使Vectorarray的尺寸灵活,以便用户可以确定尺寸以及如何确定 每个向量中必须保存许多数字。

我在这里创建了一个类数组,但是我还没有找到有关 将类的数组转换为double。

我尝试,但结果是,该数组无法转换为double。

这是我的代码:

第一页:

package menu;

public class Vector3d {

    private final float x;
    private final float y;
    private final float z;

    Vector3d(float x, float y, float z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public void Vectorprint() {

        System.out.println("(" + x + "," + y + "," + z + ")");

    }

    /**
     *
     * @param V1
     * @param V2
     */
    public void addition(Vector3d V1, Vector3d V2) {

        float value1 = V1.x + V2.x;
        float value2 = V1.y + V2.y;
        float value3 = V1.z + V2.z;

        System.out.println("The Vector after addition (" + value1 + "," + value2 + "," + value3 + ")");

    }

    public void Difference(Vector3d V1, Vector3d V2) {

        float value1 = V1.x - V2.x;
        float value2 = V1.y - V2.y;
        float value3 = V1.z - V2.z;

        System.out.println("The Vector after subtraction (" + value1 + "," + value2 + "," + value3 + ")");
    }

    public void dotProduct(Vector3d V1, Vector3d V2) {
        float value1 = V1.x * V2.x;
        float value2 = V1.y * V2.y;
        float value3 = V1.z * V2.z;
        float sum = value1 + value2 + value3;
        System.out.println("The scalarproduct is : (" + value1 + "," + value2 + "," + value3 + ")   the complete Value of Scalarproduct : " + sum);

    }

    public float multiplcation(Vector3d V, int number) { 

        float value1 = V.x * number;
        float value2 = V.y * number;
        float value3 = V.z * number;

        return value1 + value2 + value3;
    }

    public void unitVector(Vector3d v ){
      double longVector = Math.sqrt((v.x*v.x)+(v.y*v.y)+(v.z*v.z));
      double value1=v.x/longVector;
      double value2=v.y/longVector;
      double value3=v.z/longVector;


      System.out.println("the lang of this vector : " +longVector);
      System.out.println("The unitvector is : ("+ value1+","+value2+","+value3+")");
    }
}

第二页:

package menu;

import java.util.Scanner;

public class Menu {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        Vector3d v1, v2, v3;
        float element1, element2, element3, element4, element5, element6;

        System.out.println("the elements of first vector :  ");
        element1 = input.nextFloat();
        element2 = input.nextFloat();
        element3 = input.nextFloat();

        v1 = new Vector3d(element1, element2, element3);

        System.out.println("the elements of second vector :  ");
        element4 = input.nextFloat();
        element5 = input.nextFloat();
        element6 = input.nextFloat();

        v2 = new Vector3d(element4, element5, element6);
        v3 = new Vector3d(0, 0, 0);

        System.out.println("the inputed first Vector");
        v1.Vectorprint();

        System.out.println("the inputed second Vector");
        v2.Vectorprint();

        v3.addition(v1, v2);
        v3.Difference(v1, v2);
        v3.dotProduct(v1, v2);

        System.out.println("sellect a number to multiplication with Vector : ");
        int multiplicator = input.nextInt();
        System.out.println("Which Vector would you like to sellect for first input (V1) and second (V2)");
        String answer = input.next();
        if ("V1".equalsIgnoreCase(answer)) {
            System.out.println("The Vector after multiplication : " + v3.multiplcation(v1, multiplicator));
        } else if ("V2".equalsIgnoreCase(answer)) {
            System.out.println("The Vector after multiplication : " + v3.multiplcation(v2, multiplicator));
        }

        v3.unitVector(v1);
    }
}

在这里我的意思是,数组不能被转换为double:

package menu1;

import java.util.Arrays; 导入java.util.Scanner;

公共类Menu1 {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("enter the size of vektor : ");
    int vectorSize=input.nextInt();

    vector1 vectorArray[] =new vector1[vectorSize];
    vector1 object1 =new vector1();
    double convertor =vectorArray[vectorSize];
    for (int i=0;i<vectorArray.length;i++){
      vectorArray[i]=input.nextDouble();
    }
    System.out.println("the elements of the vector are :"+Arrays.toString(vectorArray));

    vector1 v1, v2, v3;
    float element1, element2, element3, element4, element5, element6;
    System.out.println("the elements of first vector :  ");
    element1 = input.nextFloat();

    element2 = input.nextFloat();

    element3 = input.nextFloat();

    v1 = new vector1(element1, element2, element3);

    System.out.println("the elements of second vector :  ");
    element4 = input.nextFloat();
    element5 = input.nextFloat();
    element6 = input.nextFloat();
    v2 = new vector1(element4, element5, element6);

    v3 = new vector1(0, 0, 0);

    System.out.println("the inputed first Vector");
    v1.Vectorprint();
    System.out.println("the inputed second Vector");
    v2.Vectorprint();
    v3.addition(v1, v2);

    v3.Difference(v1, v2);

    v3.dotProduct(v1, v2);

    System.out.println("sellect a number to multiplication with Vector : ");
    int multiplicator = input.nextInt();
    System.out.println("Which Vector would you like to sellect for first input (V1) and second (V2)");
    String answer = input.next();
    if ("V1".equalsIgnoreCase(answer)) {
        System.out.println("The Vector after multiplication : " + v3.multiplcation(v1, multiplicator));
    } else if ("V2".equalsIgnoreCase(answer)) {
        System.out.println("The Vector after multiplication : " + v3.multiplcation(v2, multiplicator));
    }

    v3.unitVector(v1);

}

}

修改后的新代码:

package menu1;

import java.util.Arrays; 导入java.util.Scanner;

公共类向量{

private final double vector1[];
private final double vector2[];
private final double vector3[];



Vector(double matrix1[], double matrix2[],double matrix3[]) {
    this.vector1 = matrix1;
    this.vector2 = matrix2;
    this.vector3 = matrix3;


}

Scanner input = new Scanner(System.in);

public void VectorInput(double Matrix[]) {

    System.out.println("input the elements of Vector :");
    for (int i = 0; i < Matrix.length; i++) {
        Matrix[i] = input.nextDouble();
    }

}

public void VectorOutput(double Matrix[]) {

    System.out.println(Arrays.toString(Matrix));

}

/**
 *
 * @param Matrix1
 * @param Matrix2
 * @param Matrix3
 */
public void addition(double Matrix1[], double Matrix2[],double Matrix3[]) {
    double Matrix4[] = new double[Matrix1.length];
    for (int i = 0; i < Matrix1.length; i++) {
        Matrix4[i] = Matrix1[i] + Matrix2[i]+Matrix3[i];
    }

    System.out.println("The Vector after addition :" + Arrays.toString(Matrix4));

}

public void Difference(double Matrix1[], double Matrix2[]) {
    double Matrix3[] = new double[Matrix1.length];
    for (int i = 0; i < Matrix1.length; i++) {
        Matrix3[i] = Matrix1[i] - Matrix2[i];
    }

    System.out.println(Arrays.toString(Matrix3));

}

public void ScalarProduct(double Matrix1[], double Matrix2[],double Matrix3[]) {
    double Matrix4[] = new double[Matrix1.length];
    for (int i = 0; i < Matrix1.length; i++) {
        Matrix4[i] = Matrix1[i] * Matrix2[i]*Matrix3[i];
    }

    System.out.println("The scalarproduct :" + Arrays.toString(Matrix4));

}

public void multiplcation(double Matrix[], int number) {
    double Matrix3[] = new double[Matrix.length];
    for (int i = 0; i < Matrix.length; i++) {
        Matrix3[i] = Matrix[i] * number;
    }

    System.out.println("The Vector after multiplcation :" + Arrays.toString(Matrix3));

}


/* I need help here , because I didn't find the right way to calculate the unitvector 
   and the result was unfortunately  wrong

   */ 
public void unitVector(double matrix[]) {
    double matrix2[]=new double[matrix.length];
    double longVector = 0;
    double result = 0;

    for (int i = 0; i < matrix.length; i++) {
        matrix2[i] = Math.sqrt(matrix[i]*matrix[i]);

    //    for (int j = 0; j < matrix2.length; j++) {
            result = matrix[i] / matrix2[i];
        }

  // }

    System.out.println("The long of the Vector :"+ Arrays.toString(matrix2));
    System.out.println("The unitvector is :" + result);

}

}

package menu1;

导入java.util.Scanner;

公共类Menu1 {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("sellect the size of vectors (maximum 6 elements):");

    int vectorSize = input.nextInt();

    if (vectorSize > 6) {
        System.err.println("Error ! , the number of elements is not acceptable ");
        return;
    }

    double Arrayvector1[] = new double[vectorSize];
    double Arrayvector2[] = new double[vectorSize];
    double Arrayvector3[] = new double[vectorSize];

    Vector object = new Vector(Arrayvector1, Arrayvector2, Arrayvector3);

    object.VectorInput(Arrayvector1);

    object.VectorInput(Arrayvector2);
    object.VectorInput(Arrayvector3);

    System.out.println("The elements of first Vector : ");
    object.VectorOutput(Arrayvector1);
    System.out.println("The elements of second Vector : ");
    object.VectorOutput(Arrayvector2);

    System.out.println("The elements of third Vector : ");
    object.VectorOutput(Arrayvector3);
    //  System.out.println("The addition of two Matrix : ");
    object.addition(Arrayvector1, Arrayvector2, Arrayvector3);

    System.out.println("First Vector -  Second vector:");
    object.Difference(Arrayvector1, Arrayvector2);

    System.out.println("Second Vector -  Firstvector:");
    object.Difference(Arrayvector2, Arrayvector1);

    object.ScalarProduct(Arrayvector1, Arrayvector2, Arrayvector3);

    System.out.println("input a number for multiplication with Vector : ");
    int multiplicator = input.nextInt();

    System.out.println("Which Vector would you like to sellect for first one input (1), second (2) and third(3)");

    String answer = input.next();
    if ("1".equalsIgnoreCase(answer)) {
        //   System.out.println("The Vector after multiplication : " );
        object.multiplcation(Arrayvector1, multiplicator);

        object.unitVector(Arrayvector1);
    } else if ("2".equalsIgnoreCase(answer)) {
        //    System.out.println("The Vector after multiplication : ");
        object.multiplcation(Arrayvector2, multiplicator);

        object.unitVector(Arrayvector2);

    } else if ("3".equalsIgnoreCase(answer)) {
        object.multiplcation(Arrayvector3, multiplicator);
        object.unitVector(Arrayvector3);
    }

}

}

1 个答案:

答案 0 :(得分:1)

好的,因此您希望能够创建一个将Vector3d对象作为元素的数组。为此,您必须使用集合。有多种类型,具体取决于您要执行的操作。基本上,集合是一个对象,用于存储或分组多个项目以及使用这些元素的方式(添加,删除,访问)。对于您的需求,您可以使用最常见的ArrayList。这是大多数程序员开始使用的默认设置,并且可以满足大多数需求。

ArrayList数组;

我不会详细介绍如何使用它,这是您需要弄清楚自己的东西。