Search code examples
javamatharithmetic-expressions

Calculate Average Java


I'm trying to calculate the average of the student scores and as it stands I'm struggling with the calculation part.

In my assignment I was asked to caulculate the avg age of three students and avg. heigth of three students.

Could someone please guide me in the last line of this code, I apologize for the newbie question, really stuck and cannot come up with any solution to why it's not calculating my numbers.

public static void main(String[] args) {
// this method will display the scores of students 

//variable declaration
int JasperAge = 20; 
int JasperHeigth = (int) 175.5;
int PaulaAge = 25; 
int PaulaHeigth = (int) 190.5;
int NicoleAge = 18;
int NicoleHeigth = (int) 165;

//output
Scanner output = new Scanner (System.in);


System.out.println("Name\t "+ " Age\t " + " Height (cm)\t");
System.out.println("\n");
System.out.println("Jasper\t  "+JasperAge+"  \t    "+JasperHeigth);
System.out.println("Paula\t  "+PaulaAge+"  \t    "+PaulaHeigth);
System.out.println("Nicole\t  "+NicoleAge+" \t    "+NicoleHeigth);
System.out.println("Average\t ((20 + 25 + 18) /3) \t ((175.5 + 190.5 + 165) /3)");
}

}


Solution

  • Your last line should probably be something like:

    System.out.println("Average age: " + ((JasperAge + PaulaAge + NicoleAge) /3) + ". Average height: " +  ((JasperHeigth + PaulaHeigth + NicoleHeigth) /3) ".");
    

    Mind my calculations, but you get the idea.