简体   繁体   中英

How do I calculate average of each subject

How do I calculate the average of each subject and average of the total value of 3 subjects and average of percentage and print them below the table.

I tried to find the average by dividing each array with number of students variable "DB" but it gives error

"The operator / is undefined for the argument type(s) int, int[][]".

And is there any way that I can get a perfect table structure for the output.

I'm using \\t to create space between columns but they do not align properly.

package cube;
  import java.io.*;
import java.util.Scanner;
  public class ReportCard
  {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int DB[][],nos=0; 
    String S="";
    Scanner s = new Scanner(System.in);
    void Input()throws Exception
    {
      System.out.print("Enter The Number Of Students : ");
      nos=Integer.parseInt(br.readLine());
      DB=new int[nos+1][20];
      String arrayOfNames[] = new String[nos];
        for (int i = 0; i < arrayOfNames.length; i++) {
            System.out.print("Enter the name of student:");
                arrayOfNames[i] = s.nextLine();


        System.out.print("\nEnter "+arrayOfNames[i]+"'s English   Score : ");
        DB[i][0]=Integer.parseInt(br.readLine());
        System.out.print("Enter "+arrayOfNames[i]+"'s Science Score : ");
        DB[i][1]=Integer.parseInt(br.readLine());
        System.out.print("Enter "+arrayOfNames[i]+"'s Maths  Score : ");
        DB[i][2]=Integer.parseInt(br.readLine());
        DB[i][3]=(int)(DB[i][0]+DB[i][1]+DB[i][2]);

        DB[i][4]=((int)((DB[i][3])*100)/300);

        DB[i][5]=(int)(DB[i][0])/DB;

        Padd("Average\t",DB[i][5]);

         if ((DB[i][0])< 50 | (DB[i][1])< 50 | (DB[i][2]) < 50) {

            System.out.print("Fail");
           }
        else {
            System.out.print("Pass");
        }
        }




      System.out.println("\n\n\nStudent Name.  English     Science  \t Maths    Total   Percentage     Pass or Fail \n");
      for(int i=0;i<nos;i++)
      {
          System.out.print(""+arrayOfNames[i]+"\t\t");Padd("English  \t ",DB[i][0]);Padd("Science  \t ",DB[i][1]);
        Padd("Maths  \t ",DB[i][2]);Padd("Total \t",DB[i][3]);Padd("Percentage\t",DB[i][4]);

if ((DB[i][0])< 50 | (DB[i][1])< 50 | (DB[i][2]) < 50) {

            System.out.print("\t\tFail");
           }
        else {
            System.out.print("\t\tPass");
        }

        System.out.println(S);

        S="";
      }

    }
    void Padd(String S,int n)
    {
      int N=n,Pad=0,size=S.length();
      while(n!=0)
      {
        n/=10;
        Pad++;
      }
      System.out.print("    "+N);
      for(int i=0;i<size-Pad-4;i++)
        System.out.print(" ");
          }


    public static void main(String args[])throws Exception
    {
      ReportCard obj=new ReportCard();
      obj.Input();

    }
  }

Your problem is in this section of code

DB[i][5]=(int)(DB[i][0])/DB;
Padd("Average\t",DB[i][5]);
if ((DB[i][0])< 50 | (DB[i][1])< 50 | (DB[i][2]) < 50) {
  System.out.print("Fail");
}
else {
  System.out.print("Pass");
}

From what I can understand from your code index 5 is not needed. You already stored the average in array index 4 (DB[i][4]).

Either way the division is illegal. An int cannot be divided by an array. Perhaps you mean the length of DB (DB.length)

You are getting this error as you are doing arithmetic operations on the array object itself.

Array in java is a data structure to hold sequential collection of values/objects. You can do arithmetic operations on elements of the array by accessing them using the index.

Like below: int result = array[i][0] / 3;

But in your code you are passing entire array as an operand in the expression.

DB[i][5]=(int)(DB[i][0])/DB;

Above line of code is causing the issue. Instead of providing any entire DB array, provide it's corresponding index according to your requirement

I hope this answers your question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM