Title: Switch Examples
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Switch Examples


import java.util.Scanner;

public class SwitchDemo {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter the month number:");
        int month = keyboard.nextInt();

        if (month ==1)
            System.out.println("January");
        else if (month ==2)
            System.out.println("February");
        else if (month==3)
            System.out.println("March");
        else if (month==4)
            System.out.println("April");
        else if (month==5)
            System.out.println("May");
        else if (month==6)
            System.out.println("June");
        else if (month==7)
            System.out.println("July");
        else if (month==8)
            System.out.println("August");
        else if (month==9)
            System.out.println("September");
        else if (month==10)
            System.out.println("October");
        else if (month==11)
            System.out.println("November");
        else if (month==12)
            System.out.println("December");
        else
            System.out.println("Bad Month Number");


        if ( month == 4 || month == 6 || month == 9 || month == 11)
            System.out.println("This month has 30 days");
        else if (month == 2)
            System.out.println("This month has 28 days");
        else System.out.println("This month has 31 days");


        System.out.println("**** Using a Switch statement *********************************************");

        switch(month)
        {
        case 1:
            System.out.println("January");
            break;
        case 2:
            System.out.println("February");
            break;
        case 3:
            System.out.println("March");
            break;
        case 4:
            System.out.println("April");
            break;
        case 5:
            System.out.println("May");
            break;
        case 6:
            System.out.println("June");
            break;
        case 7:
            System.out.println("July");
            break;
        case 8:
            System.out.println("August");
            break;
        case 9:
            System.out.println("September");
            break;
        case 10:
            System.out.println("October");
            break;
        case 11:
            System.out.println("November");
            break;
        case 12:
            System.out.println("December");
            break;
        default:
            System.out.println("Bad month number");
            break;
        }



        switch(month)
        {
        case 4:    case 6: case 9: case 11:
            System.out.println("This month has 30 days");
            break;
        case 2:
            System.out.println("This month has 28 days");
            break;
        default:
            System.out.println("This month has 31 days");
            break;
        }


    }


}





Switch issues:

1. What happens in the above example if you forget a break statement?
2. Switches can be used on integer like things: byte, int, short, long, char, also enumerated types (we will cover later in the semester)
3. See what happens if you try a double or a float
4. As of release 1.7, Java allows Strings


import java.util.Scanner;

public class Patriot {

    public static void main(String[] args) {

        System.out.println("Enter in a color");
        Scanner keyboard = new Scanner(System.in);
        String color = keyboard.next();

        switch (color)
        {
        case "blue": case "Blue":
        case "red":  case "Red":
        case "white":case "White":
            System.out.println("That color is in our flag");
            break;
        default:
            System.out.println("That color is not in our flag");
            break;
        }

    }


}


5. What is the output produced by the following code?


  char letter = 'A';
  letter += 1;
  switch(letter)
  {
    case 'A':
    case 'a':
        System.out.println("Some kind of A.");
        break;
    case 'B':
    case 'b':
        System.out.println("Some kind of B");
        break;
    default:
        System.out.println("Something else");
        break;
   }


6. In the above example, what happens if you replace the statement:
letter += 1;
with
letter = letter +1;

Java Practice(JPHF):

Create a program that prompts the user for the day of the week (1, 2, ... 7). We will consider Sunday the first day of the week. If dayOfWeek is an integer then the output of our program will be:

dayOfWeek | Program Output
1 | Today is Sunday
2 | Today is Monday
3 | Today is Tuesday
4 | Today is Wednesday
5 | Today is Thursday
6 | Today is Friday
7 | Today is Saturday

1. (5 points) Create a program using an if - else if - else if structure (See the SwitchDemo example above).
2. (5 points) Create the program again using a switch statement

Last Modified: December 23, 2014 3:29 PM