java - How to return different array values according to condition? -


i'm trying return different array values according condition. here code made error. please me find out what's problem.

private static string subject[] = {"mathematics", "english"}; private static string studentnum[] = {"1", "2"}; private static int marks[][] = {{56,51},   // student 1 mark mathermatics                                 {69,85}};  // student 2 mark english  public static double averagemarks(string acode) {      double sum[] = new double[subject.length];     double average[] = new double[subject.length];      for(int j=0;j<subject.length;j++) {         for(int i=0;i<studentnum.length;i++) {             sum[j] += marks[j][i];             average[j] = (sum[j] / studentnum.length); // average[0] average mark of mathermatics , average[1] average mark of english         }          if (acode.equals(subject[j])) {             return average[j];         }         else {             return 0;         }     }     } 

you there. line

average[j] = (sum[j] / studentnum.length); 

should out of nested for, because nested for used sum every grade, after finishes should assign sum sum[j].

also, should put return 0 @ end of function, or for loop loop 1 time (because if condition acode.equals(...) not true, return 0) in first loop.

public static double averagemarks(string acode) {     double sum[] = new double[subject.length];     double average[] = new double[subject.length];      (int j = 0; j < subject.length; j++) {         (int = 0; < studentnum.length; i++) {             sum[j] += marks[j][i];          }         average[j] = (sum[j] / studentnum.length); // average[0] average mark of mathermatics , average[1] average mark of english          if (acode.equals(subject[j])) {             return average[j];         }     }     return 0; } 

note: recommend follow java naming conventions. method should called methodname, not methodname.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -