1,105,384 Community Members

Can't quite finish GPA calculator, need help

Member Avatar
jprince
Newbie Poster
2 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey guys, I'm making a gpa calculator and almost have it completed but can't get it to read the grades and calculate the gpa properly. Any help would be great!

import java.util.Scanner;
 
public class StudentGPA {
 
 public static void main (String args[]){
 
  //User inputs the number of classes he/she has
  Scanner input = new Scanner (System.in);
  System.out.println("Please enter the amount of grades you have");
  int classes;
  classes = input.nextInt();
  String Grade = "";
  int totalCredits = 0;
  int totalCreditsEarned = 0;
  int credits;
  double gpa;
  double number=0;
  
    //String of if and else statements that set the number to the appropriate GPA
  if(Grade.equals("A")){
   number = 4.0;
  } else if(Grade.equals("A-")){
   number = 3.67;
  } else if(Grade.equals("B+")){
   number = 3.33;
  } else if(Grade.equals("B")){
   number = 3.0;
  } else if(Grade.equals("B-")){
   number = 2.67;
  } else if(Grade.equals("C+")){
   number = 2.33;
  } else if(Grade.equals("C")){
   number = 2.0;
  } else if(Grade.equals("C-")){
   number = 1.67;
  } else if(Grade.equals("D+")){
   number = 1.33;
  } else if(Grade.equals("D")){
   number = 1.0;
  } else if(Grade.equals("D-")){
   number = 0.67;
  } else if(Grade.equals("F")){
   number = 0;
  } else {
   number = 0; 
 
  //Loop that ends once the student has put information in on all his classes
  for(int count = 0; count < classes; count++)
  {
  
   //Reads the number of credits each class was
   Scanner input2 = new Scanner (System.in);
   System.out.println("Please enter the number of credits this class was (A number)");
   credits = input2.nextInt();
   //reads the letter grade using the String Grade prompt
   Scanner input3 = new Scanner(System.in);
   System.out.println("Please enter your grade for this class (Capital letters such as A, B+, C-)");
   Grade = input3.next();
 
   //algorithm for finding the GPA
   totalCredits += credits;
   totalCreditsEarned += (credits * number);
  }
 //for loop ends
  
  //GPA is calculated for all the students classes
  gpa = totalCreditsEarned / totalCredits;
 
  //GPA is printed to the screen
  System.out.println(gpa);
    }
  }
}
Member Avatar
peter_budo
Code tags enforcer
7,939 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
0
 

Suggestion, move that long if/else statement to its own method with return of double(example private static double credits(String Grade) ), when you read in grade call this method as totalCredits += credits(Grade); . See how that works for you ;)

Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 
Member Avatar
jprince
Newbie Poster
2 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I created the seperate method but isn't working just yet...what exactly do i do with the second part of totalCredits += credits(Grade);?

Member Avatar
peter_budo
Code tags enforcer
7,939 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
0
 

In your original code you used totalCredits += credits; that according to your come does algorithm for finding the GPA where earlier in the code you says that if/else statement String of if and else statements that set the number to the appropriate GPA
Therefore it was my understanding that if you want to get credits from given grade you need to run the grade through if/else statement that will provided you with a number that in my understanding is your credits.

If my assumption is not correct please correct me.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article