1

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

    }


}
3
  • 2
    Come on, did you even bother to look up how a switch statement works? Commented Sep 27, 2013 at 0:14
  • 1
    Yeah I'm going to echo Hatori. This was an easy question (which is probably why there were so many quick answers), but typically on StackOverflow you need to show a first attempt, and post when you run in to a specific problem. Commented Sep 27, 2013 at 0:18
  • Yeah I'm really sorry about my effortless questioning. It's just that this assigment was due in an hour and I had to go somewhere so I was worried I wouldnt have time to read up on it and write a program. One more time, sorry it wont happen again. Commented Sep 27, 2013 at 0:51

4 Answers 4

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

  }

}
Sign up to request clarification or add additional context in comments.

Comments

3

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.

1 Comment

Right, just did I'm new to this as you can see
2
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.");
}

Comments

-2
/* 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.

1 Comment

Hello santhosh, welcome to Stack Overflow :-) It is great to see, that you want to share your programming expertise with other programmers who are in need. Yet, when helping others it is extremly important to be accurate in your answer and only give answers if you know what you are doning. Otherwise you will most likely make the problem even bigger. Your answer sadly is programmatically and logically not correct. It will neither help the programmer, nor the users of that program to rearrange the code in the way you suggest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.