DEV Community

Anees Abdul
Anees Abdul

Posted on

Switch Example Program 2

public class Examples {
    void redSignal() {
        System.out.println("Red");
    }
    void yellowSignal(){
        System.out.println("Yellow");
    }
    void greenSignal(){
        System.out.println("Green");
    }
    void otherColor() {
        System.out.println("Not a Valid Color");
    }
    public static void main(String[] args) {
        Examples act=new Examples();
        String color= "Yellow";
        switch(color) {
        case "Red":
            act.redSignal();
            break;
        case "Yellow":
            act.yellowSignal();
            break;
        case "Green":
            act.greenSignal();
            break;
            default:
                act.otherColor();

        }

        }
        }
Enter fullscreen mode Exit fullscreen mode

O/P:

Yellow
Enter fullscreen mode Exit fullscreen mode

Top comments (0)