So I’m trying to create a program that takes in the student’s GPA and prints out if they are graduating at an honor’s level, graduating, or not graduating. I have most of the code figured out, but I am trying to make it so that ALL of the input is first, and then it will go back and print out whether each student is graduating. But what I am getting is printing out the graduation status for each student immediately after the input for that student.
I’m getting this:
Enter the number of GPAs: 3 GPA #0: 3.99 Student #0: Summa Cum Laude GPA #1: 3.1 Student #1: Graduating GPA #2: 2 Student #2: Graduating
When I want this:
Enter the number of GPAs: 3 GPA #0: 3.99 GPA #1: 3.1 GPA #2: 2 Student #0: Summa Cum Laude Student #1: Graduating Student #2: Graduating
Here is my code:
System.out.print("Enter the number of GPAs: "); int size = sc.nextInt(); int array[] = new int[size]; double gpa; for (int i = 0; i < size; i++) { System.out.print("GPA #"+ i + ": " ); gpa = sc.nextDouble(); if (gpa >= 3.90) { System.out.println("Student #" + i + ": Summa Cum Laude"); } else if (gpa >= 3.70) { System.out.println("Student #" + i + ": Magna Cum Laude"); } else if (gpa >= 3.50) { System.out.println("Student #" + i + ": Cum Laude"); } else if (gpa >= 2.0) { System.out.println("Student #" + i + ": Graduating"); } else { System.out.println("Student #" + i + ": Not graduating"); } } }}
Advertisement
Answer
First you need to fill the array with values GPA
we declare an array of double
double array[] = new double[size];
We iterate through for loop and we ask user to enter GPA and we fill our array with those values
for (int i = 0; i < size; i++) { System.out.print("GPA #" + i + ": "); gpa = sc.nextDouble(); array[index++] = gpa; }
Now we have array with our values filled so what should we do know we print it by checking array values with our if statements.
for(int i=0;i<array.length;i++){ if (array[i] >= 3.90) { System.out.println("Student #" + i + ": Summa Cum Laude"); } else if (array[i] >= 3.70) { System.out.println("Student #" + i + ": Magna Cum Laude"); } else if (array[i] >= 3.50) { System.out.println("Student #" + i + ": Cum Laude"); } else if (array[i] >= 2.0) { System.out.println("Student #" + i + ": Graduating"); } else { System.out.println("Student #" + i + ": Not graduating"); } } } }
FULL CODE
System.out.print("Enter the number of GPAs: "); int size = sc.nextInt(); double array[] = new double[size]; double gpa; int index = 0; for (int i = 0; i < size; i++) { System.out.print("GPA #" + i + ": "); gpa = sc.nextDouble(); array[index++] = gpa; } for(int i=0;i<array.length;i++){ if (array[i] >= 3.90) { System.out.println("Student #" + i + ": Summa Cum Laude"); } else if (array[i] >= 3.70) { System.out.println("Student #" + i + ": Magna Cum Laude"); } else if (array[i] >= 3.50) { System.out.println("Student #" + i + ": Cum Laude"); } else if (array[i] >= 2.0) { System.out.println("Student #" + i + ": Graduating"); } else { System.out.println("Student #" + i + ": Not graduating"); } } } }