java - Requiring user to enter only 1 char for a letter grade for a gpa program -
i trying finish java assignment , stuck on last bit. need make when user enters letter grade in order calculate g.p.a. can enter 1 letter.
for example, need receive error if enter aaa
instead of a
.
i stuck on how go doing this. works except 1 thing. new java great.
here class:
public class gpa { private int sumcredits; private int sumpoints; public int getpointsforgrade(char letter) { int gradepoints; switch (letter) { case 'a': case 'a': gradepoints = 4; break; case 'b': case 'b': gradepoints = 3; break; case 'c': case 'c': gradepoints = 2; break; case 'd': case 'd': gradepoints = 1; break; case 'f': case 'f': gradepoints = 0; break; default: gradepoints = -1; break; } return gradepoints; } public void constructor(){ sumcredits = 0; sumpoints = 0; return; } public void addgrade( char letter, int credits){ sumcredits = sumcredits + credits; sumpoints = sumpoints + getpointsforgrade(letter) * credits; } public double calcgpa(){ double gpa; if (sumcredits == 0){ return 0; } gpa = (double)sumpoints / sumcredits; return gpa; } public int totalcredits(){ return sumcredits; } public int totalpoints(){ return sumpoints; } }
and here test class, or gpa calculator
import java.util.*; import java.util.scanner; public class computegpa { public static void main (string [] args){ scanner scan = new scanner(system.in); gpa gpa = new gpa(); int courses; int credits; char letter; while (true){ system.out.println(" enter number of courses: "); courses = scan.nextint(); if (courses > 0){ break; } else { system.out.println(" invalid number of courses - must greater 0"); } } (int = 1; i<= courses; i++) { while (true){ system.out.println(" enter grade ( 1 character )"); string grade = scan.next(); letter = grade.touppercase().charat(0); if (letter == 'a' || letter == 'b' || letter == 'c' || letter =='d' || letter == 'f' ){ break; } else { system.out.println(" must enter a,b,c,d,f (upper or lower case)"); } if (letter != 'a' || letter != 'b' || letter != 'c' || letter != 'd' || letter != 'f'){ system.out.println("must 1 letter only"); } } while (true){ system.out.println(" enter credits: "); credits = scan.nextint(); if (credits < 0 || credits > 9){ system.out.println("invalid credits = must between 0 , 9, inclusively"); } else { break; } } gpa.addgrade(letter, credits); system.out.println(" points: " + gpa.getpointsforgrade(letter) ); system.out.println(" sum points: " + gpa.totalpoints() + " sum credits: " + gpa.totalcredits()); } system.out.printf(" gpa: %.2f%n", gpa.calcgpa()); } }
you check see if there more entries in input. did not run code, simple way check.
system.out.println(" enter grade ( 1 character )"); string grade = scan.next(); letter = grade.touppercase().charat(0); if(grade.length() > 1){ // check size of input system.out.println("you entered many letters"); } else if (letter == 'a' || letter == 'b' || letter == 'c' || letter =='d' || letter == 'f' ){ break; } else { system.out.println(" must enter a,b,c,d,f (upper or lower case)"); }
Comments
Post a Comment