Reputation: 39
Beginner at Java here. So I'm stuck as to how I would go about making it so if the user input is "A+" I will get 4.0 and no higher? Similarly for "F", I want to make it so that F,F-,F+ all result in 0.0 gpa. I figured either I was going to put special parameters for letter grades A and F or just code a "limit" or a "cap" so to speak on the GPA. What I mean is making it so my double 'gpa' can only be values 0-4.0. Either method that you think works best is fine, I'm just so unfamiliar with the syntax and the vocabulary of Java that I don't know where to even begin.
Here's my code:
import java.util.*;
public class HomeworkCalculator {
public static void main(String[] args) {
Scanner in = new Scanner(System. in );
String grade, letter1, letter2;
System.out.print("Enter a letter grade: ");
grade = in .nextLine();
letter1 = grade.substring(0, 1);
letter2 = grade.substring(1, 2);
double gpa = 0;
if (letter1.equals("A")) {
gpa = 3.9;
} else if (letter1.equals("B")) {
gpa = 3.0;
} else if (letter1.equals("C")) {
gpa = 2.1;
} else if (letter1.equals("D")) {
gpa = 1.2;
} else if (letter1.equals("F")) {
gpa = 0;
}
if (letter2.equals("+")) {
System.out.println(gpa + 0.3);
} else if (letter2.equals("-")) {
System.out.println(gpa - 0.3);
}
}
}
I tried individually coding the user input 'A+' to be only equal to 4.0 at the end of the program but then the output just gives me 4.0 and then 4.2 after that when I only want 1 value. I then tried adding a break or a return to see if I could make it so that IF the input was 'A+' it would automatically just end the code right then and there -- hopefully stopping any additional outputs from happening, but that didn't happen.
Any suggestions?
Upvotes: 0
Views: 4028
Reputation: 26
Naive solution here:
package testgrade;
import java.util.*;
public class Tg {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String grade;
System.out.print("Enter a letter grade: ");
grade = in.nextLine();
double gpa = 0.0;
boolean bad_entry = false;
int entered_grade_len = grade.length();
// Verify entered content
if (entered_grade_len <= 2 && entered_grade_len > 0) {
if((grade.substring(0).toUpperCase() == "A") ||
(grade.substring(0).toUpperCase() == "B") ||
(grade.substring(0).toUpperCase() == "C") ||
(grade.substring(0).toUpperCase() == "D") ||
(grade.substring(0).toUpperCase() == "F"))
{
switch(entered_grade_len)
{
case 2:
if((grade.substring(1) == "+") ||
(grade.substring(0).toUpperCase() == "-"))
break;
default:
bad_entry = true;
break;
}
}
}
if(bad_entry)
{
System.out.println("Usage: Enter grades in the normal spectrum from A(+/-) to F");
System.out.println("eg: A+, B-, D+, C, F...");
return;
}
String lettergrade = grade.substring(0,1).toString().toUpperCase();
switch (lettergrade) {
case "A":
gpa = 4.0;
break;
case "B":
gpa = 3.0;
break;
case "C":
gpa = 2.0;
break;
case "D":
gpa = 1.0;
break;
case "F":
gpa = 0.0;
break;
default:
break;
}
if (grade.length() > 1) {
lettergrade = grade.substring(1, 2);
switch (lettergrade) {
case "+":
if (gpa <= 3 && gpa >= 1.0)
gpa += 0.3;
break;
case "-":
if (gpa <= 4 && gpa >= 1.0)
gpa -= 0.3;
break;
default:
break;
}
}
System.out.println(gpa);
in.close();
}
}
This could be improved a ton with some regular expressions for the comparisons, but for quick and dirty it works. (A = 3.9? bah...but you could do as above, if gpa = 4 and grade.length() = 1) gpa -= 0.1)
Upvotes: 0
Reputation: 5755
Another possibility is that at the end of the entire section of code, you could add:
if (gpa > 4) gpa = 4;
if (gpa < 0) gpa = 0;
if (letter1.equals("F") && letter2.equals("+")) gpa = 0;
Upvotes: 2
Reputation: 5755
if (letter1.equals("A") && letter2.equals("+")) gpa = 4.0;
else if(letter1.equals("A"))
{gpa = 3.9;}
else if(letter1.equals("B"))
{gpa = 3.0;}
else if(letter1.equals("C"))
{gpa = 2.1;}
else if(letter1.equals("D"))
{gpa = 1.2;}
else if(letter1.equals("F"))
{gpa = 0;}
if (!letter1.equals("F")) {
if(letter2.equals("+") && !letter1.equals("A")) gpa += 0.3;
else if(letter2.equals("-")) gpa -= 0.3;
}
Upvotes: 0