如何将2个数组加在一起并获得总和的平均值?

时间:2021-11-12 23:04:15

i'm trying to add 2 arrays together and get the average of the sum how do i do this? i want to produce the answers in a array aswell.

我正在尝试将2个阵列加在一起并获得总和的平均值我该怎么做?我想在阵列中产生答案。

public static void part1 (){

    double examMarks [] = {50,40,60,80,70,11};
    double courseworkmarks [] = {65,49,58,77,35,40};

    System.out.println ("These are the exam marks and the course work marks");//First row is the exam marks, second row is the course work marks
    computeMarks (examMarks);
    computeMarks1 (courseworkmarks);

}
public static void computeMarks(double[] examMarks)
{
    for (int row=0;row<examMarks.length;row++){
            System.out.print (examMarks[row] +"\t");
        }
    System.out.println();
    }
public static void computeMarks1(double[] courseworkmarks)
{
    for (int row=0;row<courseworkmarks.length;row++){
            System.out.print (courseworkmarks[row] +"\t");
        }
    System.out.println();
    }

4 个解决方案

#1


2  

You can try something like following

您可以尝试以下内容

    double examMarks [] = {50,40,60,80,70,11};
    double courseworkmarks [] = {65,49,58,77,35,40};

    double avgMarks[] =new double[examMarks.length];

    for(int i=0;i<avgMarks.length;i++){
        avgMarks[i]=(examMarks[i]+courseworkmarks[i])/2;
    }

#2


0  

You can define a array with the length = array.length + array2.length;

您可以使用length = array.length + array2.length定义一个数组;

And use System.arraycopy to copy the values.

并使用System.arraycopy复制值。

Below is a sample method for combining the 2 arrays.

下面是组合2个数组的示例方法。

public double[] copyTwoArrays(double[] arrayOne, double[] arrayTwo)
{
    if(null == arrayOne || arrayOne.length == 0)
    {
        return arrayTwo;
    }

    if( null == arrayTwo || arrayTwo.length == 0)
    {
        return arrayOne;
    }       
    double[] result = new double[arrayOne.length + arrayTwo.length];

    System.arraycopy(arrayOne, 0, result, 0, arrayOne.length);
    System.arraycopy(arrayTwo, 0, result, arrayOne.length, arrayTwo.length);

    return result;
}

Take your arrays for example:

以您的阵列为例:

double examMarks [] = {50,40,60,80,70,11};
double courseworkmarks [] = {65,49,58,77,35,40};

You can use the following code to populate the combined array.

您可以使用以下代码填充组合数组。

double[] result = someInstance.copyTwoArrays(examMarks, courseworkmarks);

#3


0  

You can use a function like

你可以使用像这样的功能

 public static void totalMarks(double[] examMarks, double[] courseworkmarks){
      double total[] = new double[6];
      double totalMarks = 0;

      System.out.println("================================================");
      for(int i = 0;i < examMarks.length;i++){
      total[i]=examMarks[i] + courseworkmarks[i];
      totalMarks = totalMarks+total[i];
          System.out.print(total[i]+"\t");
      }
      System.out.println("========================================");
      System.out.println("total marks are "+totalMarks);
      System.out.println("average is "+(totalMarks/examMarks.length));
     // total;
  }

If you need you can break it into two parts for total and for average

如果您需要,可以将其分为两部分,总计和平均值

#4


0  

Have your method return an array. You can do something like this

让你的方法返回一个数组。你可以做这样的事情

public static double[] computeMarks(double[] examMarks)
{
    int[] newArray = new int[examMarks.length];

    for (int row=0;row<examMarks.length;row++){
        newArray[i] = (examMarks[row] + courseworkmarks[row])  / 2;
    }

    return newArray

}

To print

public static void main(String[] args){

    double[] array = computeMarks(yourArray);

    for (int i  = 0; i < array.length; i++){
        System.out.println(array[i] + "\t");
    }
}

#1


2  

You can try something like following

您可以尝试以下内容

    double examMarks [] = {50,40,60,80,70,11};
    double courseworkmarks [] = {65,49,58,77,35,40};

    double avgMarks[] =new double[examMarks.length];

    for(int i=0;i<avgMarks.length;i++){
        avgMarks[i]=(examMarks[i]+courseworkmarks[i])/2;
    }

#2


0  

You can define a array with the length = array.length + array2.length;

您可以使用length = array.length + array2.length定义一个数组;

And use System.arraycopy to copy the values.

并使用System.arraycopy复制值。

Below is a sample method for combining the 2 arrays.

下面是组合2个数组的示例方法。

public double[] copyTwoArrays(double[] arrayOne, double[] arrayTwo)
{
    if(null == arrayOne || arrayOne.length == 0)
    {
        return arrayTwo;
    }

    if( null == arrayTwo || arrayTwo.length == 0)
    {
        return arrayOne;
    }       
    double[] result = new double[arrayOne.length + arrayTwo.length];

    System.arraycopy(arrayOne, 0, result, 0, arrayOne.length);
    System.arraycopy(arrayTwo, 0, result, arrayOne.length, arrayTwo.length);

    return result;
}

Take your arrays for example:

以您的阵列为例:

double examMarks [] = {50,40,60,80,70,11};
double courseworkmarks [] = {65,49,58,77,35,40};

You can use the following code to populate the combined array.

您可以使用以下代码填充组合数组。

double[] result = someInstance.copyTwoArrays(examMarks, courseworkmarks);

#3


0  

You can use a function like

你可以使用像这样的功能

 public static void totalMarks(double[] examMarks, double[] courseworkmarks){
      double total[] = new double[6];
      double totalMarks = 0;

      System.out.println("================================================");
      for(int i = 0;i < examMarks.length;i++){
      total[i]=examMarks[i] + courseworkmarks[i];
      totalMarks = totalMarks+total[i];
          System.out.print(total[i]+"\t");
      }
      System.out.println("========================================");
      System.out.println("total marks are "+totalMarks);
      System.out.println("average is "+(totalMarks/examMarks.length));
     // total;
  }

If you need you can break it into two parts for total and for average

如果您需要,可以将其分为两部分,总计和平均值

#4


0  

Have your method return an array. You can do something like this

让你的方法返回一个数组。你可以做这样的事情

public static double[] computeMarks(double[] examMarks)
{
    int[] newArray = new int[examMarks.length];

    for (int row=0;row<examMarks.length;row++){
        newArray[i] = (examMarks[row] + courseworkmarks[row])  / 2;
    }

    return newArray

}

To print

public static void main(String[] args){

    double[] array = computeMarks(yourArray);

    for (int i  = 0; i < array.length; i++){
        System.out.println(array[i] + "\t");
    }
}