Results 1 to 4 of 4
- 11-19-2012, 02:38 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 5
- Rep Power
- 0
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, 03:26 PM #2
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.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 11-19-2012, 03:32 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 5
- Rep Power
- 0
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, 08:12 PM #4
Re: I want to convert this If-Else Statement program to Switch Statement
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
The Switch Statement
By Everyman in forum New To JavaReplies: 11Last Post: 04-20-2012, 03:29 PM -
More with the Switch Statement
By Everyman in forum New To JavaReplies: 15Last Post: 04-19-2012, 03:24 PM -
the switch statement and unreachable statement error
By name in forum New To JavaReplies: 2Last Post: 03-26-2012, 04:27 PM -
help with switch statement
By java__beginner in forum New To JavaReplies: 4Last Post: 03-19-2009, 02:22 PM -
Switch Statement Help
By bluegreen7hi in forum New To JavaReplies: 6Last Post: 02-06-2008, 05:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks