第一个小型题目:日历

import java.util.Scanner;

public class canlendar {

 /**
  * 判断是否为闰年
  *
  * @param year
  * @return
  */
 public static boolean isLeapYear(int year) {
  if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
   return true;
  } else
   return false;

 }

 /**
  * 根据传入的年月,计算该月有多少天
  *
  * @param year
  * @param month
  * @return
  */
 public static int monthOfDays(int year, int month) {
  if (month == 2) {
   if (isLeapYear(year)) {
    return 29;
   } else
    return 28;
  } else if (month < 7 && month % 2 != 0 || month > 7 && month % 2 == 0) {
   return 31;
  } else
   return 30;

 }

 /**
  * 根据提供的年月,计算是该月1号是该年中的第几天
  *
  * @param year
  * @return
  */
 public static int crossDays(int year,int month) {
  int sumCrossDays = 0;
  for (int i = 1; i < month; i++) {
   sumCrossDays += monthOfDays(year, i);
  }
  return sumCrossDays;

 }

 /**
  * 求1900年到輸入的年份一共多少天
  * @param year
  * @return
  */
 public static int newCrossDays(int year) {
  int sumdays = 0;
  for (int i = 1900; i < year; i++) {
   if (isLeapYear(i)) {
    sumdays += 366;
   } else
    sumdays += 365;
  }
  return sumdays;
 }

 
 
 public static void main(String[] args) {
  Scanner scan = new Scanner(System.in);
  
  System.out.println("请输入一个年份:");
  int years = scan.nextInt();
  System.out.println("请输入一个月份:");
  int month = scan.nextInt();
  int sumDays = newCrossDays(years);
  System.out.println("請輸入一個日期:");
  int day = scan.nextInt();
  System.out.println("一 二 三 四 五 六 七"); 
  int week=1;;
  week=(newCrossDays(years)+crossDays(years,month)+day+1)%8+1;
   //System.out.println("你輸入的是星期"+week);
  int sum=week+monthOfDays(years,month);
  int[] dates=new int[sum];
  for(int k=0;k<=week;k++){
   dates[k]=-1;
  }
  int index=1;
  for(int number=week;number<sum;number++){
   dates[number]=index;
   index++;
  }
  for(int k=0;k<dates.length;k++){
   if(dates[k]==-1){
    System.out.print(" ");
   }else
    System.out.print(dates[k]+" ");
   if((k+1)%7==0){
    System.out.println();
  }
  }
  
   

 }
}

原文地址:https://www.cnblogs.com/smile-zcc/p/5559716.html