表驱动法——直接访问表示例1
《代码大全》看到“表驱动法”一章,以下是表驱动法的第一个方法——直接访问表
import java.util.Scanner; import java.util.Calendar;class DaysPerMonth {public static void main(String[] args) {System.out.println("输入年份:");Scanner scan = new Scanner(System.in);String input_year = scan.nextLine();int year = Integer.parseInt(input_year);System.out.println("输入月份:");String input_month = scan.nextLine();int month = Integer.parseInt(input_month);int days = daysPerMonth(year, month);System.out.println( year + " 年 " + month + " 月有 " + days + "天");}public static int daysPerMonth(int year, int month) {boolean flag = LeapYearIndex(year);System.out.println("是否是闰年?" + flag);if(flag) {int[] days = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};return days[month-1];} else {int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};return days[month-1];}}public static boolean LeapYearIndex(int year) {if ( ( year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0 ) {return true;} else {return false;}}}
posted on 2017-08-23 23:37 tzzt01 阅读(...) 评论(...) 编辑 收藏