Results 1 to 4 of 4
  1. #1
    UmairBaloch is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default 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");
    }

    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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!

  3. #3
    UmairBaloch is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default 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

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: I want to convert this If-Else Statement program to Switch Statement

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. The Switch Statement
    By Everyman in forum New To Java
    Replies: 11
    Last Post: 04-20-2012, 04:29 PM
  2. More with the Switch Statement
    By Everyman in forum New To Java
    Replies: 15
    Last Post: 04-19-2012, 04:24 PM
  3. Replies: 2
    Last Post: 03-26-2012, 05:27 PM
  4. help with switch statement
    By java__beginner in forum New To Java
    Replies: 4
    Last Post: 03-19-2009, 03:22 PM
  5. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 06:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •