0

我正在尝试将 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

4 回答 4

2

您可以尝试以下操作

    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;
    }
于 2013-11-08T04:56:39.597 回答
0

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

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

}

打印

public static void main(String[] args){

    double[] array = computeMarks(yourArray);

    for (int i  = 0; i < array.length; i++){
        System.out.println(array[i] + "\t");
    }
}
于 2013-11-08T05:01:29.803 回答
0

您可以定义一个长度为 = array.length + array2.length; 的数组。

并使用System.arraycopy复制值。

下面是组合 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;
}

以您的数组为例:

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

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

double[] result = someInstance.copyTwoArrays(examMarks, courseworkmarks);
于 2013-11-08T05:03:16.180 回答
0

您可以使用类似的功能

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

如果你需要,你可以把它分成两部分,总计和平均

于 2013-11-08T05:12:06.160 回答