Question

In: Computer Science

Please write a Java program using a for loop with a switch statement to enter 5...

Please write a Java program using a for loop with a switch statement to enter 5 quiz scores. Each should be out of ten points and give the corresponding grade A, B , C etc.

Solutions

Expert Solution

Screenshot

---------------------------------------------------------------

Program

import java.util.Scanner;

public class GradeCalculator {

   public static void main(String[] args) {
       //array to store scores
       int quizScore[]=new int[6];
       int totalScore=0;
       //Scanner object for input read
       Scanner sc=new Scanner(System.in);
      
       //Loop for 5 grades
       for(int i=0;i<5;i++) {
           //Prompt for input
           System.out.print("Enter quiz "+(i+1)+" score out of 10: ");
           int score=sc.nextInt();
           //Input validation
           while(score<0 || score >10) {
               System.out.println("Error!!!Score should be 0...10");
               System.out.print("Enter quiz "+(i+1)+" score out of 10: ");
               score=sc.nextInt();
           }
           quizScore[i]=score;
           totalScore+=score;
       }
      
       quizScore[5]=totalScore;
       //Loop display grades
       for(int i=0;i<6;i++) {
           switch(quizScore[i]) {
           case 9:
           case 10:
               System.out.println("Quiz "+(i+1)+" has A grade");
               break;
           case 45:
           case 46:
           case 47:
           case 48:
           case 49:
           case 50:
               System.out.println("Total grade is A grade");
               break;
           case 8:
               System.out.println("Quiz "+(i+1)+" has B grade");
               break;
           case 40:
           case 41:
           case 42:
           case 43:
           case 44:
               System.out.println("Total grade is B grade");
               break;
           case 7:
               System.out.println("Quiz "+(i+1)+" has C grade");
               break;
           case 35:
           case 36:
           case 37:
           case 38:
           case 39:
               System.out.println("Total grade is C grade");
               break;
           case 6:
               System.out.println("Quiz "+(i+1)+" has D grade");
               break;
           case 30:
           case 31:
           case 32:
           case 33:
           case 34:
               System.out.println("Total grade is D grade");
               break;
           default:
               if(i!=5) {
                   System.out.println("Quiz "+(i+1)+" has F grade");
               }
               else {
                   System.out.println("Total grade is F grade");
               }
              
               break;
           }
       }
   }

}

-----------------------------------------------------------------

Output

Enter quiz 1 score out of 10: 9
Enter quiz 2 score out of 10: 8
Enter quiz 3 score out of 10: 9
Enter quiz 4 score out of 10: 8
Enter quiz 5 score out of 10: 7
Quiz 1 has A grade
Quiz 2 has B grade
Quiz 3 has A grade
Quiz 4 has B grade
Quiz 5 has C grade
Total grade is B grade


Related Solutions

USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the...
USING THE SWITCH STATEMENT - Write a java program that asks the user to enter the number of their favorite month of the year – obviously, that would be 1 – 12. Write a switch statement that takes the number and converts it to the fully spelled out name [ex. 3 would be MARCH] . Be sure to build in error message to catch any invalid data entries such as 0 or 13 etc. Print out the number that was...
Write a program using switch statement that asks user to enter the month number and then...
Write a program using switch statement that asks user to enter the month number and then it prints out the number of days for that month. For simplicity reasons have your program print 28 days for February no matter if it is a leap year or not. Your program should also handle any invalid month numbers that user could enter (hint use default for the switch). Use a while loop to allow user to test for different month entries till...
Write a program using the switch statement to calculate geometric quantities. Prompt the user to enter...
Write a program using the switch statement to calculate geometric quantities. Prompt the user to enter a radius. Then present a menu of choices for quantities to be calculated from that radius:                         A         Area of a Circle                         C         Circumference of a Circle                         S          Surface Area of a Sphere                         V         Volume of a Sphere Prompt the user to enter the character corresponding to the quantity to be calculated. Use a switch statement to handle the calculations. Use...
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
For this assignment you will write a Java program using a loop that will play a...
For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class in that project named GuessANumber.java for this assignment. The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user has guessed the...
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items as indicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemo program. Then print the total bill at the end Use the readInput() method for the following input data Oranges: 10 for...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1 and 100 inclusive. The user will be repeatedly prompted until they input a valid integer.
Write a program that produces the following output using nested for loop in Java. ****** ////////////...
Write a program that produces the following output using nested for loop in Java. ****** //////////// ****** ***** //////////\\ ***** **** ////////\\\\ **** *** //////\\\\\\ *** ** ////\\\\\\\\ ** * //\\\\\\\\\\ * \\\\\\\\\\\\
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT