Home » Java » Switch statements for more than one parameter

Switch statements for more than one parameter

Posted by: admin July 23, 2018 Leave a comment

Questions:

I’m trying to write a program that uses a switch statement to call methods from another class, with user input being the control for the switch, as below:

switch (args[0]) {
    case "bark":
        Dog.bark(); 
        break;
}

but would it be possible for a switch statement to work if parameters needed to be passed into bark? If bark took in, say a color, can you make the case “bark red” and pass red into the bark function which may be defined as Dog.bark(color)? Or is this not possible in java? Thank you!

edit: My actual code is a bit above this example, where certain methods that the switch statement will call can’t be called without parameters. I wanted to know if it was possible for the switch statement to take in that second argument and pass it in during the cases.

Answers: