# I want to convert this If-Else Statement program to Switch Statement

• 11-19-2012, 03:38 PM
UmairBaloch
I want to convert this If-Else Statement program to Switch Statement
Hi, I've made this program so far and it displays the correct output, thing i want to do is to convert the If_else thing into switch statement. Any help will be highly appreciated.
import java.util.Scanner;

class Result{
public static void main(String a[]){
Scanner scan = new Scanner(System.in);
int S_1, S_2, S_3;
double percentage, obtainedMarks, totalMarks = 300;

System.out.print("Enter the marks obtained in First Subject: ");
S_1 = scan.nextInt();

System.out.print("Enter the marks obtained in Second Subject: ");
S_2 = scan.nextInt();

System.out.print("Enter the marks obtained in Third Subject: ");
S_3 = scan.nextInt();

obtainedMarks = S_1 + S_2 + S_3;
System.out.println("Obtained Marks are: " + obtainedMarks);

percentage = ((obtainedMarks / totalMarks) * 100);
System.out.println("Percentage is: " + percentage + "Percent");

if (percentage>=90){
System.out.print(" A+ , Exceptional");
} else
if (percentage>=80 && percentage<=90){
System.out.print(" A , Excellent");
} else
if (percentage>=70 && percentage<=80){
System.out.print(" B+ , Very Good");
} else
if (percentage>=60 && percentage<=70){
System.out.print(" B , Good");
} else
if (percentage>=50 && percentage<=60){
System.out.print(" C+ , Satisfactory");
} else
if (percentage<=50){
System.out.print(" D , Fail");
}

}
}
• 11-19-2012, 04:26 PM
KevinWorkman
Re: I want to convert this If-Else Statement program to Switch Statement
Why do you want to convert it to a switch statement? You're using compound boolean statements, which don't exactly translate to cases.

Although, you can probably eliminate about half of those boolean expressions.
• 11-19-2012, 04:32 PM
UmairBaloch
Re: I want to convert this If-Else Statement program to Switch Statement
Actually, my professor assigned this to me, I mean three subject, and their marks, total, percentage and on the basis of percentage we need to grade. he said to do this with if-else and switch statement both. so as you are saying how can i make cases in it, do i have to write the whole program from the beginning
• 11-19-2012, 09:12 PM
DarrylBurke
Re: I want to convert this If-Else Statement program to Switch Statement