/* MultiwayBranch6.java David G. Klick September 21, 2007 CIS 111 Demonstrates switch statement with fall-through (purposefully missing 'break' statements). This version takes leap years into account. This version also has some basic input error checking. */ public class MultiwayBranch6 extends CIS111App { public static void main(String[] args) { int days = 0; int month = 0; int year = 0; boolean valid = true; // get month and check for validity month = getInt("What month number is it? "); if ( (month < 1) || (month > 12)) { println("Invalid month specified"); valid = false; } // get year and check for validity if (valid) { year = getInt("What year is it? "); if (year < 1900) { println("Year must be 1900 or greater"); valid = false; } } // do number of days in month calculation if (valid) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: if ( ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0)) { days = 29; } else { days = 28; } break; default: // should never get here since we have a previous check println("*Invalid month specified*"); valid = false; } } // display results if (valid) { print(month + "/" + year + " has " + days + " days"); } } }