【问题标题】:How do I add 2 arrays together and get the average of the sum?如何将 2 个数组加在一起并获得总和的平均值?
【发布时间】:2013-11-08 04:50:58
【问题描述】:

我正在尝试将 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();
    }

【问题讨论】:

  • 我正在尝试添加考试成绩和课程成绩。请注意,我需要添加第一列,然后获取平均值和第二列,然后获取平均值。所以 50+65、40+49、60+58 等等。
  • 您的解决方案现在有什么问题?

标签: java arrays eclipse average


【解决方案1】:

你可以试试下面的方法

    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 个不同的长度。在这种情况下可能很好,但一个好的做法应该是确保数组长度不会导致 ArrayOutOfBoundsException
  • @RUJordan - 如果数组的长度不同,那么 OP 的目标将毫无意义。它只为长度相等的数组定义。
  • “这种情况可能没问题”
【解决方案2】:

你可以定义一个数组,长度 = array.length + array2.length;

并使用 System.arraycopy 复制值。

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

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

【讨论】:

    【解决方案3】:

    你可以使用类似的函数

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

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 我相信这就是你的意思 (examMarks[row]+courseworkmarks[row]) / 2;
      • 是的,你是对的。根据 OP 请求,我只是提供了更多返回数组的示例。但是对于问题的最终解决方案,OP 可能需要它。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 2021-06-03
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多