164k views
3 votes
Create a Java program that inputs a grade from the user. The grade input from the user will be an integer. Once the input is stored, use an if-else-if block of code to determine the letter grade of the inputted integer grade. Do not use a bunch of if statements by themselves to solve this problem. You will print an error message for inputs greater than 100 and for inputs less than 0. Both errors must be handled together by a compound conditional statement that is joined by a short circuit AND or OR operator. Assign your error message to a String variable and use that variable in a System.out.println statement to print the error. You must assign a char variable for each letter grade. Use System.out.printf to print the letter grade. Run your program several times using these inputs and ensure that you are receiving these outputs: input: 140 expected output: ERROR - You have entered an invalid input. Input: -45 expected output: ERROR - You have entered an invalid input. input: 82 expected output: You have earned the letter grade B. Run it a few more times using various other inputs. Part II Convert the if-else-if code block to a switch statement to solve the problem. Use modulus and/or integer division to convert the grade input so that the range of grades are converted to one value. All other requirements from Part I are still required for part two. Use the same inputs and the outputs should remain the same. My Code import java.util.Scanner; int grade; System.out.print("Enter your grade: "); grade = scnr.nextInt(); if (grade >= 90) { System.out.println("You have earned the letter grade A."); } else if (grade >= 80) { System.out.println("You have earned the letter grade B."); } else if (grade >= 70) { System.out.println("CYou have earned the letter grade C."); } else if (grade >= 60) { System.out.println("You have earned the letter grade D."); } else if (grade <= 59) { System.out.println("You have earned the letter grade F."); } else (0 >= grade >= 100) { System.out.println("You have entered an invalid input.") }

1 Answer

4 votes

Answer:

Public static void main (string args []){

System.out.println("The grade is" + print grade (78));

System.out.println("The grade is" + print grade (59));

}

Public static char print grade (integer grade){

If ( grade >= 90)

System.out.println("You have earned the letter grade A");

Else if (grade >=80)

System.out.println("You have earned the letter grade B");

Else if (grade >=70)

System.out.println("You have earned the letter grade C");

Else if (grade >=60)

System.out.println("You have earned the letter grade D");

Else if (grade <=59)

System.out.println("You have earned the letter grade F");

else (0 >= grade >= 100) System.out.println("You have entered an invalid input.")

}

}

Step-by-step explanation:

User Iqmaker
by
7.1k points