2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > Java实验项目二——打印某年某月日历

Java实验项目二——打印某年某月日历

时间:2019-12-27 13:52:31

相关推荐

Java实验项目二——打印某年某月日历

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 { 1112//定义方法,判断一个年是否是闰年 13public static boolean ifLeapyear(int year) { 1415 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} 2627//判断某年某月有多少天 28public static int getDays(String yearMonth) { 2930 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}; 3536 if( Operate.ifLeapyear(year) ) { 37 return monthOne[month-1]; 38 } 39 return monthTwo[month-1]; 40} 4142//接收用户的输入 43public static String getInput() { 4445 Scanner scan = new Scanner(System.in); 46 String yearMonth = ""; 47 System.out.println( "请输入年月,中间用'-'分开:" ); 48 yearMonth = scan.next(); 4950 return yearMonth; 51} 5253//验证用户输入数据的合法性 54public static boolean validate(String yearMonth) { 5556 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} 7677//计算某年某月距离1990年有多少天 78public 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 } 8990 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}102103104//打印日历105public 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 //打印出rili131 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}141142 }

1 /* 2 * Description:打印某年某月的日历 3 * 4 * Written By:Cai 5 * 6 * Date Written:-09-24 7 * 8 * */ 9 10 package main;11 import tools.Operate;12 13 14 public class DemoTwo2 {15 16public 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); //打印rili32}33 }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。