标签:style ann ring system bool i++ scan -- 定义
Program:打印万年历(输入年份,月份,输出该月的日历,已知1900年1月1日是星期一),
要 求:
(1)编写一个方法判断闰年;
(2)编写一个方法判断某年某月有多少天;
(3)编写一个方法计算某年某月前距离1900年1月1日的总天数;(4)编写一个输出某年某月日历的方法;
(5)编写一个测试方法。
Description:该项目由多个类实现,最后类为main方法的调用。代码如下:
1 /* 2 *Description:定义工具类 3 * 4 * */ 5 6 package tools; 7 8 import java.util.Scanner; 9 10 public class Operate { 11 12 //定义方法,判断一个年是否是闰年 13 public static boolean ifLeapyear(int year) { 14 15 if( year % 400 == 0 ) { //能被400整除 16 17 return true; 18 }else if( year % 100 != 0 && year % 4 == 0 ) { //不是100倍数,但是能被4整除 19 20 return true; 21 }else { 22 23 return false; 24 } 25 } 26 27 //判断某年某月有多少天 28 public static int getDays(String yearMonth) { 29 30 String array[] = yearMonth.split("-"); 31 int year = Integer.parseInt( array[0] ); 32 int month = Integer.parseInt( array[1] ); 33 int monthOne[] = {31,29,31,30,31,30,31,31,30,31,30,31}; 34 int monthTwo[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 35 36 if( Operate.ifLeapyear(year) ) { 37 return monthOne[month-1]; 38 } 39 return monthTwo[month-1]; 40 } 41 42 //接收用户的输入 43 public static String getInput() { 44 45 Scanner scan = new Scanner(System.in); 46 String yearMonth = ""; 47 System.out.println( "请输入年月,中间用‘-‘分开:" ); 48 yearMonth = scan.next(); 49 50 return yearMonth; 51 } 52 53 //验证用户输入数据的合法性 54 public static boolean validate(String yearMonth) { 55 56 if( yearMonth.matches( "\\d{4}\\W\\d{1,2}" ) ) { //进行正则匹配 57 58 String str[] = yearMonth.split("-"); 59 60 if( Integer.parseInt( str[0] ) < 1900 ) { //年份小于1990 61 return true; 62 } 63 64 //月份不在1-12月之间 65 if( Integer.parseInt(str[1]) < 1 || Integer.parseInt( str[1] ) > 12 ) { 66 67 return true; 68 } 69 70 return false; 71 72 }else { 73 return true; 74 } 75 } 76 77 //计算某年某月距离1990年有多少天 78 public static int getAllDays(int year,int month,int[] monthOne,int[] monthTwo) { 79 int count = 0; 80 for( int i = 1900;i < year; i++ ) { 81 82 if( Operate.ifLeapyear(i) ) { 83 count += 366; 84 }else { 85 86 count += 365; 87 } 88 } 89 90 for( int i = 0; i < month - 1; i++ ) { 91 92 if( Operate.ifLeapyear(year) ) { 93 94 count += monthOne[i]; 95 }else { 96 97 count += monthTwo[i]; 98 } 99 } 100 return count; 101 } 102 103 104 //打印日历 105 public static void printDate(String yearMonth) { 106 String array[] = yearMonth.split("-"); 107 int year = Integer.parseInt( array[0] ); 108 int month = Integer.parseInt( array[1] ); 109 int monthOne[] = {31,29,31,30,31,30,31,31,30,31,30,31}; //闰年每个月的天数 110 int monthTwo[] = {31,28,31,30,31,30,31,31,30,31,30,31}; //平年每个月的天数 111 int count = 0; 112 113 count = Operate.getAllDays(year, month, monthOne, monthTwo); //得到天数 114 115 int remainDays = count % 7; //除以7。看看剩余的天数 116 int monthDays = Operate.getDays(yearMonth); //得到目标月份的天数 117 String[] days = new String[remainDays + monthDays + 2]; //开辟一个String数组,存放打印的内容 118 119 int k = 1; 120 for(int i = 0; i < days.length; i++) { //为数组赋值 121 122 if( i < remainDays + 1 ) { 123 days[i] = " "; 124 }else { 125 days[i] = k + ""; 126 k++; 127 } 128 } 129 130 //打印出rili 131 System.out.println( "---------------------" + year + "年" + month + "月" + "---------------------" ); 132 System.out.println( "日\t一\t二\t三\t四\t五\t六" ); 133 for( int i = 0; i < days.length; i++ ) { 134 135 System.out.print(days[i] + "\t"); 136 if( (i + 1) % 7 == 0 ) { //每七天换行 137 System.out.println(); 138 } 139 } 140 } 141 142 }
1 /* 2 * Description:打印某年某月的日历 3 * 4 * Written By:Cai 5 * 6 * Date Written:2017-09-24 7 * 8 * */ 9 10 package main; 11 import tools.Operate; 12 13 14 public class DemoTwo2 { 15 16 public static void main(String args[]) { 17 18 String input = ""; //接收用户输入的值 19 boolean flag = true; 20 while( flag ) { //判断用户输入的日期是否合法 21 input = Operate.getInput(); 22 if( Operate.validate(input) ) { //调用方法检测 23 24 System.out.println( "输入日期格式错误,请重新输入:" ); 25 }else { 26 27 flag = false; 28 } 29 } 30 31 Operate.printDate(input); //打印rili 32 } 33 }
标签:style ann ring system bool i++ scan -- 定义
原文地址:http://www.cnblogs.com/caizhen/p/7592584.html