write grade program using switch statement in java

In this core java programming tutorial we will write a program calculates marks on basis of given grades in java using switch statement.


Write a program which calculates marks on basis of given grades in java using switch statement
* if Grade A then  marks >=80
* if Grade B then marks >=60 and less than 80
* if Grade C then marks >=40 and less than 60
* if Grade F then marks <=40
* if any other grade is passed then print invalid grade




Full Program/SourceCode / Example - which calculates marks on basis of given grades in java using switch statement >
import java.util.Scanner;
/*
* Write a program which calculates marks on basis of given grades in java using switch statement
* if Grade A then  marks >=80
* if Grade B then marks >=60 and less than 80
* if Grade C then marks >=40 and less than 60
* if Grade F then marks <=40
* if any other grade is passed then print invalid grade
*/
/** JavaMadeSoEasy.com */
public class GradeProgramUsingSwitchStatementExample {
   public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          System.out.print("Enter grade from (A, B, C or F) : ");
          String str = scanner.next();
          char grade = str.charAt(0);
          switch (grade) {
                 case 'A':
                       System.out.println("Grade A - marks >=80");
                       break;
  
                 case 'B':
                       System.out.println("Grade B - marks >=60");
                       break;
                 case 'C':
                       System.out.println("Grade C - marks >=40");
                       break;
                      
                 case 'F':
                       System.out.println("Grade F - marks <40 FAIL");
                       break;
                      
                 default : //optional
                       System.out.println("Invalid Grade");
                       break; //optional
          }
   }
}
/*OUTPUT 1
Enter grade from (A, B, C or F) : A
Grade A - marks >=80
*/
/*OUTPUT 2
Enter grade from (A, B, C or F) : B
Grade B - marks >=60
*/
/*OUTPUT 3
Enter grade from (A, B, C or F) : C
Grade C - marks >=40
*/
/*OUTPUT 4
Enter grade from (A, B, C or F) : F
Grade F - marks <40 FAIL
*/



So, in this core java programming tutorial we wrote a program which calculates marks on basis of given grades in java using switch statement.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>


















No comments:

Post a Comment