My homework task is to create a program that accepts a numeric input between 0-100 and returns a letter and +/- if appropriate. We are to accomplish this using nested if statements. I attempted to create an outer if statement that would return a letter grade followed by nested if statements that would return the +/- part.
The output I receive varies from 6543 to 00. I have copied my code below. Could anyone point me in the right direction? I feel like this is a bit of a mess.
import java.util.Scanner;
import java.lang.Math;
public class Grade {
public static void main (String [] args) {
Scanner scan = new Scanner (System.in);
//Prompt user to enter grade
System.out.println("Please enter your grade ");
int grade = scan.nextInt();
byte grade1 = (0);
byte note = (0);
//Determine letter and +/-
if ( grade <= 100 && grade >= 90 ) {
grade1 = 'A';
if (grade <= 100 && grade >= 96) {
note = '+';
}
else if (grade <= 94 && grade >= 90) {
note = '-';
}
}
else if ( grade <= 80 && grade >= 89 ) {
grade1 = 'B';
if (grade <= 89 && grade >= 86) {
note = '+';
}
else if (grade <= 84 && grade >= 80) {
note = '-';
}
}
else if ( grade <= 70 && grade >= 79 ) {
grade1 = 'C';
if (grade <= 79 && grade >= 76) {
note = '+';
}
else if (grade <= 74 && grade >= 70) {
note = '-';
}
}
else if ( grade <= 60 && grade >= 69 ) {
grade1 = 'D';
if (grade <= 69 && grade >= 66) {
note = '+';
}
else if (grade <= 64 && grade >= 60) {
note = '-';
}
}
else if ( grade <= 59 ) {
grade1 = 'F';
}
//Print out grade
System.out.println("You have a " + grade1 + note + " in the class.");
// End program
scan.close();
System.exit(0);
}
}
else if ( grade <= 80 && grade >= 89 ) {
Take a moment to think about that one. I think you intend for e.g. 85
to cause this if to trigger, right.
Is 85 lower or equal than 80? I don't think it is. It's not 89 or higher either. In fact, no number will ever satisfy this condition.
Flip your <
and >
signs :)
Second issue is that grade1 and note are bytes, which are numbers, so "You have a " + (some number) + (some other number) + " in the class" is always going to print "You have a 12345677 in the class", that is, those things are, well, numbers. I have no idea why you thought byte
was going to work out here. Give it another try with char
.