/* MultiwayBranch6.java CIS 160 David G. Klick 2007-09-25 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. */ import java.util.Scanner; public class MultiwayBranch6 { public static void main(String[] args) { int days = 0; int month = 0; int year = 0; boolean valid = true; Scanner kbd = new Scanner(System.in); // get month and check for validity System.out.print("What month number is it? "); month = kbd.nextInt(); if ( (month < 1) || (month > 12)) { System.out.println("Invalid month specified"); valid = false; } // get year and check for validity if (valid) { System.out.print("What year is it? "); year = kbd.nextInt(); if (year < 1900) { System.out.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 System.out.println("*Invalid month specified*"); valid = false; } } // display results if (valid) { System.out.print(month + "/" + year + " has " + days + " days"); } } }