17.9k views
3 votes
Print GPA Write a method named printGPA that takes in as a parameter a Scanner to read in user input, and calculates a student's grade point average. The user will type in a number of courses, followed by that many integer grade. Then, the method will print out the average of all grades entered. Here are two example dialogues:

User Macarthur
by
5.9k points

1 Answer

4 votes

import java.util.Scanner;

import java.util.ArrayList;

public class MyClass {

public static void printGPA(Scanner scan){

System.out.println("How many courses are you taking?");

int numCourses = scan.nextInt();

ArrayList<Integer> list = new ArrayList<Integer>();

int i = 1;

while (i <= numCourses){

System.out.println("What's your GPA in course "+i);

int element = scan.nextInt();

list.add(element);

i++;

}

double avg = 0;

for (int w : list){

avg += w;

}

System.out.println("Your average GPA for all your courses is " + (avg / numCourses));

}

public static void main(String args[]) {

Scanner scan = new Scanner(System.in);

printGPA(scan);

}

}

I hope this helps!

User Tuomassalo
by
5.1k points