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

必要に応じて、合計と平均の 2 つの部分に分割できます

于 2013-11-08T05:12:06.160 に答える
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 に答える