I am writing a GPA calculator for a java programming class. I have written all the code and tried to run the program but I keep getting the most insane GPA's. 0.00 13.00 etc. While I would love to have a 13.00 GPA, I think there is something perhaps wrong with my code. I have posted the code below. I have also included some system.out.prints of different variables throughout the process trying to diagnose what is wrong. I will delete these in the final code but thought they would perhaps be useful for diagnostic purposes. Below is the code and the terminal screen. Also I apologize if my variables are confusing. I need to work on defining them better. For your info, Ctotal is the total number of class hours and Gtotal is the number of GPA points for each class added together. hours is the number of hours for an individual class. grade is the letter grade while Ngrade is that converted into number form. Ex A(grade) = 4.0(Ngrade). Please ask if anything does not make sense. I appreciate the help with troubleshooting. I have tried several things but can not figure out what is wrong. I guess it is at least putting out a GPA now even if it is obviously incorrect
Code
import java.text.*; import java.util.*; public class proj3 { public static void main (String[] args){ DecimalFormat df = new DecimalFormat("#0.00"); Scanner s = new Scanner(System.in); System.out.print("How many courses did you take?"); int course = Integer.parseInt(s.nextLine()); int count = 0; int Ngrade = 0; int Ctotal = 0; int Gtotal = 0; while (count < course){ count ++; System.out.print("How many hours?"); int hours = Integer.parseInt(s.nextLine()); System.out.print("Letter Grade?"); String grade = s.nextLine(); if (grade.equals( "A" )) { Ngrade = 4; } if (grade.equals( "B" )) { Ngrade = 3; } if (grade.equals("C" )) { Ngrade = 2; } if (grade.equals("D" )) { Ngrade = 1; } if (grade.equals("F" )) { Ngrade = 0; } if (grade.equals( "a")) { Ngrade = 4; } if (grade.equals("b")) { Ngrade = 3; } if (grade.equals( "c")) { Ngrade = 2; } if (grade.equals("d")) { Ngrade = 1; } if (grade.equals( "f")) { Ngrade = 0; } // Gtotal is A = 4.0 b=3.0 etc //Ctotal is number of class hours System.out.println(Gtotal + "initial GPA value"); System.out.println(Ctotal + "initial class hours"); Gtotal= Gtotal * Ctotal; System.out.println(Gtotal + "GPA when multiplied by class hours"); //Gtotal equals gpa times class hours Ctotal= Ctotal + hours; Gtotal = Gtotal + Ngrade; System.out.println(Ctotal + "total number of class hours"); System.out.println(Gtotal + "total of GPA's for all classes"); } double GPA = Gtotal/Ctotal; System.out.println(df.format(GPA)); } }
Terminal window
How many courses did you take?4
How many hours?1
Letter Grade?A
0initial GPA value
0initial class hours
0GPA when multiplied by class hours
1total number of class hours
4total of GPA's for all classes
How many hours?2
Letter Grade?B
4initial GPA value
1initial class hours
4GPA when multiplied by class hours
3total number of class hours
7total of GPA's for all classes
How many hours?3
Letter Grade?C
7initial GPA value
3initial class hours
21GPA when multiplied by class hours
6total number of class hours
23total of GPA's for all classes
How many hours?4
Letter Grade?D
23initial GPA value
6initial class hours
138GPA when multiplied by class hours
10total number of class hours
139total of GPA's for all classes
13.00 (This is Semester GPA)