As others have pointed out, you can implement your switching cases inside a method that takes a parameter that matches the desired switch case. These switching cases can recursively call the same method with the required switching cases. Here is a complete working example that allows you to check if a year is a leap year or not:
public class LeapYear { static int year = 2016; public static void main(String[] args) { leapSwitch(1); } public static void leapSwitch(int switchcase) { switch (switchcase) { case 1: { if (year % 4 == 0) { leapSwitch(2); } else { leapSwitch(5); } } break; case 2: { if (year % 100 == 0) { leapSwitch(3); } else { leapSwitch(4); } } break; case 3: { if (year % 400 == 0) { leapSwitch(4); } else { leapSwitch(5); } } break; case 4: { System.out.println(year+ " is a leap year!"); } break; case 5: { System.out.println("Not a leap year..."); } break; } } }
source share