0

How can I build my for loop in this GPA Calculator? Brand new to Java, and for our class we are required to ask for user's grade and credit hours 4 times, then calculate their average. We were instructed to use switch statements and loops, but not much other instruction. We just learned about for, while, and do loops. Here is what I have so far, not sure what to do next.

This is what output is supposed to look like.

public static void main(String[] args) {
    String grade = "";
    int creditHour = 0;
    int pointsPerCourse = 0;
    double gpa = 0.0;
    
    Scanner scnr = new Scanner(System.in);
    
    System.out.println("\t\t\tpointsPerCourse Calculator");
    System.out.println("This program will calculate pointsPerCourses based on course grades");
    
    for ();
    {
        System.out.print("Enter grade: ");
        grade = scnr.nextLine();
        
        System.out.print("Enter number of credits for grade: ");
        creditHour = Integer.parseInt(scnr.nextLine());
    }
        
    switch (grade) {
        case "A":
            pointsPerCourse = 4 * creditHour;
        case "B":
            pointsPerCourse = 3 * creditHour;
        case "C":
            pointsPerCourse = 2 * creditHour;
        case "D":
            pointsPerCourse = 1 * creditHour;
        default:
            pointsPerCourse = 0 * creditHour;
    }   
            
    gpa = (double) pointsPerCourse / creditHour;
    System.out.printf("The GPA is %.1f ", gpa);

    scnr.close();
}
0009laH
  • 1,960
  • 13
  • 27

2 Answers2

0

This is not golang you cant use a for loop like that and you want to put your switch case inside the loop. Someone sugested a while loop but since you only want to loop over 4 times, a proper for loop will do it for you.

for(int i = 0, i < 4, i++){
 System.out.print("Enter grade: ");
    grade = scnr.nextLine();
    
    System.out.print("Enter number of credits for grade: ");
    creditHour = Integer.parseInt(scnr.nextLine());
    
    switch (grade) 
    {
    
      case "A":
        pointsPerCourse = 4 * creditHour;
      case "B":
        pointsPerCourse = 3 * creditHour;
      case "C":
        pointsPerCourse = 2 * creditHour;
      case "D":
        pointsPerCourse = 1 * creditHour;
        
      default:
        pointsPerCourse = 0 * creditHour;
     }
}
  • Ah I see, thank you! What I was confused about was what to put in the for loop, I thought that I was supposed to put some kind of string in there but I have to stick with the "int i" variables. – ryanmonty42 Oct 28 '21 at 22:05
0

I think you are trying to do this: loop 4 times and add th epoints and credit hours for averaging

import java.util.Scanner;

class Prog {
    public static void main(String[] args) {

        String grade = "";
        int creditHour = 0;
        int creditHourSum = 0;
        int pointsPerCourse = 0;
        double gpa = 0.0;

        Scanner scnr = new Scanner(System.in);

        System.out.println("\t\t\tpointsPerCourse Calculator");
        System.out.println("This program will calculate pointsPerCourses based on course grades");

        for (int i = 0; i < 4; i++) {
            System.out.print("Enter grade: ");
            grade = scnr.nextLine();

            System.out.print("Enter number of credits for grade: ");
            creditHour = Integer.parseInt(scnr.nextLine());
            creditHourSum += creditHour;

            switch (grade) {

                case "A":
                    pointsPerCourse += 4 * creditHour;
                    break;
                case "B":
                    pointsPerCourse += 3 * creditHour;
                    break;
                case "C":
                    pointsPerCourse += 2 * creditHour;
                    break;
                case "D":
                    pointsPerCourse += 1 * creditHour;
                    break;
                default:
                    pointsPerCourse += 0 * creditHour;
            }
            System.out.println(pointsPerCourse);
        }

        gpa = (double) pointsPerCourse / creditHourSum;
        System.out.println(pointsPerCourse + ", " + creditHourSum);
        System.out.printf("The GPA is %.1f ", gpa);

        scnr.close();

    }
}

OUTPUT

This program will calculate pointsPerCourses based on course grades
Enter grade: A
Enter number of credits for grade: 4
16
Enter grade: E
Enter number of credits for grade: 4
16
Enter grade: B
Enter number of credits for grade: 4
28
Enter grade: C
Enter number of credits for grade: 3
34
34, 15
The GPA is 2.3
arp5
  • 169
  • 10