Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By JosAH

Thread: I need am converting an " if else statement" to a "switch statement"

  1. #1
    Mulk123 is offline Member
    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0

    Default I need am converting an " if else statement" to a "switch statement"

    I clueless on how to do this


    my code is


    import java.util.Scanner;
    public class mylab

    {
    public static void main(String[] args)

    { Scanner in = new Scanner(System.in);

    int month;
    int day;
    String season= "seasons";


    System.out.print("type a two digit month");
    System.out.print(" and day");
    month = in.nextInt();
    day = in.nextInt();


    String fall = " fall";
    String winter = " winter ";
    String summer = " summer";
    String spring = " spring";

    System.out.print(" Month="+ month +" Day= "+day);

    if( month <= 3)
    { System.out.println(" Winter");
    season= winter; }
    else if ( month <=6)
    { System.out.println(" Spring ");
    season=spring; }

    else if ( month<= 9)
    { System.out.println(" Summer ");
    season= spring; }

    else if ( month<=12)
    { System.out.println(" Fall");
    season= fall; }



    I just need to change the bolded part to a switch


    if (month % 3 == 0 && day >= 21)
    {
    if ( season.equals(winter) )
    System.out.println(" Spring");

    else if ( season.equals(spring) )
    System.out.println ( "Summer" );

    else if ( season.equals(summer) )
    System.out.println ( " Fall");

    else if ( season.equals(winter) )
    System.out.println( " winter");

    }

    }
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,525
    Rep Power
    5

    Default Re: I need am converting an " if else statement" to a "switch statement"

    First, the vernal equinox occurs around the 20th of March so your day is wrong in your if statement. Switch statements do not readily lend themselves to tests that involve ranges. The easiest way is to enumerate all the months.

    Regards,
    Jim
    Last edited by jim829; 01-30-2014 at 04:32 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    178
    Rep Power
    0

    Default Re: I need am converting an " if else statement" to a "switch statement"

    Here is a link to some good switch statement info and examples
    The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    The issue that Jim is talking about is that switch statements work by cases

    For example, here is a sample switch statement
    Java Code:
    int a = 12;
    switch (a) {
    case 4:
    System.out.println("The number is 4!");
    break;
    
    case 7:
    System.out.println("The number is 7!");
    break; 
    
    case 12:
    System.out.println("The number is 12!");
    break;
    
    default: 
    System.out.println("I don't know what the number is...");
    }
    This should print out "The number is 12!".

    Notice 2 things - first of all, notice that in a switch statement, brackets only go around the OUTSIDE. Individual cases don't have brackets.

    Second, there needs to be a break in each case - this ends the checking for the switch.

    Also, notice that you can only check for a single case.
    In other words, you can NOT do
    Java Code:
    case 5-8: dothis; break;   //you can't do this
    I'm assuming that you need to make it into a switch for a homework assignment.

    A shortcut, so that you don't have to have a case for each number, is this:

    If a set of numbers trigger a single result, you can do this:
    Java Code:
    case 1:
    case 2:
    case 3:
    dothis;
    break;
    instead of

    Java Code:
    case 1:
    dothis;
    break;
    
    case 2:
    dothis;
    break;
    
    case 3:
    dothis;
    break;
    They both do the same thing


    One last hint to help you with your homework.

    If I remember correctly, the switch statement will check from top to bottom. Once a case is found, it will "break" afterwards and not keep looking. Think about this to figure out the order that you'll want to put your cases in.
    Last edited by AlexGraal; 01-30-2014 at 04:52 AM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default Re: I need am converting an " if else statement" to a "switch statement"

    Quote Originally Posted by AlexGraal View Post
    In other words, you can NOT do
    Java Code:
    case 5-8: dothis; break;   //you can't do this
    Well, as a matter of fact, you can do that, but it probably won't do what you wanted, i.e. it switches on value -3 ...

    kind regards,

    Jos
    masijade likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. An "if" statement inside a "for" loop?
    By soccermiles in forum New To Java
    Replies: 18
    Last Post: 04-20-2010, 03:44 AM
  4. Replies: 3
    Last Post: 05-25-2009, 08:50 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

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