zaynv
zaynv

Reputation: 255

Converting an if-else-if statement into a switch statement in java

I'm having trouble turning this program from an if-else-if statement into a switch statement. Any help would be appreciated.

import java.util.Scanner;

public class ifToSwitchConversion {


    public static void main(String [] args) {

        // Declare a Scanner and a choice variable
        Scanner stdin = new Scanner(System.in);
        int choice = 0;

        System.out.println("Please enter your choice (1-4): ");
        choice = stdin.nextInt();

        if(choice == 1)
        {
            System.out.println("You selected 1.");
        }
        else if(choice == 2 || choice == 3)
        {
            System.out.println("You selected 2 or 3.");
        }
        else if(choice == 4)
        {
            System.out.println("You selected 4.");
        }
        else
        {
            System.out.println("Please enter a choice between 1-4.");
        }

    }


}

Upvotes: 1

Views: 41955

Answers (4)

santhosh
santhosh

Reputation: 1

/* Just change choice to 1
 * if you want 2 or 3 or 4
 * just change the switch(2 or 3 or 4) 
 */

switch(1)
{
    case 1:
        System.out.println("You selected 1.");
        break;
    case 2:
    case 3:
        System.out.println("You selected 2 or 3.");
        break;
    case 4:
        System.out.println("You selected 4.");
        break;
    default:
        System.out.println("Please enter a choice between 1-4.");
}

Answer : You selected 1.

Upvotes: -2

MeIr
MeIr

Reputation: 7316

import java.util.Scanner;

public class ifToSwitchConversion {

public static void main(String [] args) {

    // Declare a Scanner and a choice variable
    Scanner stdin = new Scanner(System.in);
    int choice = 0;

    System.out.println("Please enter your choice (1-4): ");
    choice = stdin.nextInt();


    switch(choice) {
        case 1:
            System.out.println("You selected 1.");
            break;
        case 2:
        case 3:
            System.out.println("You selected 2 or 3.");
            break;
        case 4:
            System.out.println("You selected 4.");
            break;
        default:
            System.out.println("Please enter a choice between 1-4.");
    }

  }

}

Upvotes: 4

arshajii
arshajii

Reputation: 129497

You probably want something like:

switch (choice) {
    case 1:
        System.out.println("You selected 1.");
        break;
    case 2:
    case 3:  // fall through
        System.out.println("You selected 2 or 3.");
        break;
    case 4:
        System.out.println("You selected 4.");
        break;
    default:
        System.out.println("Please enter a choice between 1-4.");
}

I urge you to read the switch statement tutorial, which should explain how/why this works as it does.

Upvotes: 3

Idan Arye
Idan Arye

Reputation: 12603

switch(choice)
{
    case 1:
        System.out.println("You selected 1.");
        break;
    case 2:
    case 3:
        System.out.println("You selected 2 or 3.");
        break;
    case 4:
        System.out.println("You selected 4.");
        break;
    default:
        System.out.println("Please enter a choice between 1-4.");
}

Upvotes: 2

Related Questions