user1081122
user1081122

Reputation:

Making GPA calculator in java, need a little adjusting to finish it

Ok so I've done pretty much all of it but can't figure out why the grades being put in aren't converting to the corresponding numbers and actually computing the gpa. If someone could please try and fix it so that it works properly, that would be awesome! Thank you.

here is the code I have:

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);
}
}

}`

Upvotes: 1

Views: 12716

Answers (1)

Andy
Andy

Reputation: 21

easy fix, put your if block in the loop.

before the algorith for finding the gpa

Upvotes: 1

Related Questions