打印日历

直接上代码:

import java.util.Scanner;

public class OutputCalendar {
public static void main(String [] args){
boolean judge=true;
while(judge){
int year,month,crossDay,yearDay=0,monthDay=0,day;
System.out.print("请输入年份(1900-2100):");
Scanner input=new Scanner(System.in);
year=input.nextInt();
if(year<1900||year>2100){
System.out.println("输入错误,请重新输入");
}
else{
System.out.print("清输入月份(1-12):");
month=input.nextInt();
if(month<1||month>12){
System.out.println("输入错误,请重新输入");
}
else{
judge=false;
for (int i = 1900; i < year; i++){
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0){
yearDay += 366;
}
else{
yearDay += 365;
}
}
for (int i = 1; i < month; i++){
if (i == 2){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
monthDay += 29;
}
else{
monthDay += 28;
}
}
else if (i <= 7 && i % 2 != 0 || i > 7 && i % 2 == 0){
monthDay += 31;
}
else{
monthDay += 30;
}
}
if(month==2){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
day = 29;
}
else{
day = 28;
}
}
else if (month <= 7 && month % 2 != 0 || month > 7 && month % 2 == 0){
day = 31;
}
else{
day = 30;
}
crossDay =yearDay + monthDay;
int week=crossDay%7+1;
System.out.println("**************************************************");
System.out.println("日\t一\t二\t三\t四\t五\t六");
for(int i=1;i<=week;i++){
System.out.print("\t");
}
for(int i=1;i<=day;i++){
System.out.print(i+"\t");
if((week+i)%7==0){
System.out.println();
}
}
System.out.println();
System.out.println("**************************************************");
input.close();
}
}
}
}
}

综合实例打印日历,对初学者来说,还是有点难度的,步骤有点多,多打几遍能加深映像,理解其中的运行过程。

posted @ 2017-03-19 18:12  听风似见  阅读(224)  评论(0编辑  收藏  举报