Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SchoolGPA{
- public static void main(String[] args){
- //declare and initialize variables
- double cGPA = 0.0, pGPA = 0.0, sGPA = 0.0;
- int cStudents = 0, pStudents = 0, sStudents = 0;
- double cHigh = 0.0, pHigh = 0.0, sHigh = 0.0;//start high at a low value so immediately overwritten
- double cLow = 10.0, pLow = 10.0, sLow = 10.0;//start low at a high value so immediately overwritten
- Scanner kb = new Scanner(System.in);
- //Get first school and gpa
- System.out.print("Enter school (C/P/S) and GPA: ");
- String school = kb.next();
- double gpa = kb.nextDouble();
- //loop while not done
- while( ! school.equals("done") ){
- //Canton
- if( school.equals("C") ){
- cGPA += gpa;//add current gpa to Canton gpa running total
- cStudents++;//increment number of Canton students
- if( gpa > cHigh ) cHigh = gpa;//if current gpa is bigger than high, update high
- if( gpa < cLow ) cLow = gpa;//if current gpa is lower than low, update low
- }
- //Plymouth
- else if( school.equals("P") ){
- pGPA += gpa;
- pStudents++;
- if( gpa > pHigh ) pHigh = gpa;
- if( gpa < pLow ) pLow = gpa;
- }
- //Salem
- else if( school.equals("S") ){
- sGPA += gpa;
- sStudents++;
- if( gpa > sHigh ) sHigh = gpa;
- if( gpa < sLow ) sLow = gpa;
- }
- //Not C/P/S entered so invalid school
- else{
- System.out.println("Invalid school. Enter C/P/S");
- }
- //Prompt and get next school and gpa
- System.out.print("Enter school (C/P/S) and GPA: ");
- school = kb.next();
- gpa = kb.nextDouble();
- }//end of while loop
- //Print table at end (outside loop)
- System.out.println("\t\t\tAverage\tHigh\tLow");
- System.out.println( "Canton\t" + (cGPA/cStudents) + "\t\t" + cHigh + "\t" + cLow);
- System.out.println( "Plymouth\t" + (pGPA/pStudents) + "\t\t" + pHigh + "\t" + pLow);
- System.out.println( "Salem\t\t" + (sGPA/sStudents) + "\t\t" + sHigh + "\t" + sLow);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement