Thread: Trying to calculate GPA

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    2

    Trying to calculate GPA

    Code:
    import
    Code:
     java.util.Scanner; 
    
    publicclass gpa {
    
       publicstaticvoid main(String[] args){
          double gpa=0;
          double input =0;
          String grade="";
          int classes =0;
          double fail =0;
          do
          {
             System.out.print("Enter Grade (n to quit): "); 
             Scanner sc =newScanner(System.in); 
              grade = sc.nextLine(); 
              grade = grade.toLowerCase(); 
              if(grade.equals("a"))
              {
                 input =4.0;
              }
              if(grade.equals("b"))
              {
                 input =3.0;
              }
              if(grade.equals("c"))
              {
                 input =2.0;
              }
              if(grade.equals("d"))
              {
                 input =1.0;
              }
              if(grade.equals("f"))
              {
                 fail =1;
              }
    
              gpa+=input;
              classes++;
          }while(grade!="n");
          System.out.print("GPA: "+ gpa +"   "); 
    
          gpa/=classes;
          if(gpa>=2&& classes >=4&& fail !=1)
          {
             System.out.print("Eligible");    
          }
          elseif(classes <4)
          {
             System.out.print("Ineligible. less than 4 classes"); 
          }
          elseif(gpa <2.0)
          {
             System.out.print("Ineligible. GPA is less than 2.0"); 
          }
          elseif(gpa >=2.0&& fail ==1)
          {
             System.out.print("Ineligible. GPA is above 2.0, but has an F");
          }
          elseif(gpa <2.0&& fail ==1)
          {
             System.out.print("Ineligible. GPA is below 2.0 and has F");
          }
       }
    
    }
    


    Here's what I need to be able to output:

    1. Eligible
    2. Ineligible, taking less than 4 classes
    3. Ineligible, gpa below 2.0
    4. Ineligible, gpa above 2.0 but has F grade (note: gpa >= 2.0)
    5. Ineligible, gpa below 2.0 and has F grade

    It keeps asking for input. How do I stop this? I tried converting grade into a char but that ended horribly for me. I think it might be a compiler error or something. Any help would be appreciated. Though helpful help would keep my blood pressure down.

    Source: https://gpahub.net/


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,059
    Asking on a Java forum might be a good idea.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculate E
    By hyper nova in forum C Programming
    Replies: 4
    Last Post: 03-10-2016, 07:25 AM
  2. calculate 100! bug.
    By nimitzhunter in forum C++ Programming
    Replies: 11
    Last Post: 03-29-2014, 11:50 PM
  3. Calculate BMI and BMR
    By nguystep in forum C Programming
    Replies: 4
    Last Post: 10-26-2011, 02:59 AM
  4. calculate age from date
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-22-2005, 08:57 AM
  5. calculate the ln of a number?
    By willc0de4food in forum C Programming
    Replies: 9
    Last Post: 04-04-2005, 04:39 PM

Tags for this Thread