编写一个程序,输出一个月的日历。

方法一:

import java.util.Calendar;
import java.util.Scanner;


public class YueLi {

 
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  int a = sc.nextInt();//a表示你将要输入几个年月
  sc.nextLine();
  Calendar calendar = Calendar.getInstance();
  for(int i = 0; i < a; i++){
   String s = sc.nextLine();
   String[] strs = s.split("-");
   int year = Integer.parseInt(strs[0]);
   int month = Integer.parseInt(strs[1]);
   calendar.set(Calendar.YEAR, year);   //设置年
   calendar.set(Calendar.MONTH, month-1);
   calendar.set(Calendar.DATE, 1);
   //System.out.println(calendar.get(7));
   for(int j =1; j < calendar.get(Calendar.DAY_OF_WEEK); j++) {
    System.out.print("   ");//输出开始前面的空格
   }
   for(int k = 1; k <= whichMonth(year, month); k++) {
    calendar.set(Calendar.DATE, k);
    if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
     System.out.println();
    if(k <= 10) { //为了输出对齐
    System.out.print(k+"  ");
    } else {
     System.out.print(k+ " ");
    }
    
   }
  }
  

 }
 public static int whichMonth(int year, int month) {
  int days = 0;
  switch (month){
       case 1:
       case 3:
       case 5:
       case 7:
       case 8:
       case 10:
       case 12:
        days = 31;
       break;
       case 4:
       case 6:
       case 9:
       case 11:
        days = 30;
       break;
       case 2:
        if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {//润年
         days = 29;
        } else {
         days = 28;
        }
        break;
       }

  return days;
 }
}
方法二:

import java.util.Scanner;

public class DisplayYueLi{
  public static void main(String[] args){
   Scanner sc=new Scanner(System.in);
   String input1=sc.nextLine();
   String[] input2=input1.split("-");
    int year=Integer.parseInt(input2[0]);
    int month=Integer.parseInt(input2[1]);
    int fristDay=GetfristDay(year,month);//
    int mDay=GetmDay(year,month);
    DisplayYueLi(fristDay,mDay);
  }
  static int GetfristDay(int year,int month){
   int zDays=0;
   for(int i=1;i<year;i++){
     if(i%4==0&&i%100!=0||i%400==0){
       zDays=zDays+366;
     }
     else{
       zDays=zDays+365;
     }
   }
    for(int j=1;j<month;j++){
    switch(j){
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
    zDays=zDays+31;
    break;
    case 4:
    case 6:
    case 9:
    case 11:
    zDays=zDays+30;
    break;
    case 2:
    if(year%4==0&&year%100!=0||year%400==0){
       zDays=zDays+29;
     }
     else{
       zDays=zDays+28;
     }
    }
   }
    int fristDay=(int)(zDays+1)%7;
    // System.out.println("fristDay=" +fristDay);
    return(fristDay);
  }
  static int GetmDay(int year,int month){
    int mDay=0;
     switch(month){
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
      mDay=31;
    break;
    case 4:
    case 6:
    case 9:
    case 11:
      mDay=30;
    break;
    case 2:
    if(year%4==0&&year%100!=0||year%400==0){
       mDay=29;
     }
     else{
       mDay=28;
     }break;
    }
    return(mDay);
   }
   static void DisplayYueLi(int fristDay,int mDay){
    String space="          ";
    System.out.println(space+"日"+space+"一"+space+"二"+space+"三"+space+"四"+space+"五"+space+"六");
    for(int m=0;m<fristDay;m++){
     System.out.print(space+"  ");
   }
    for(int n=1;n<=mDay;n++){
      if((n+fristDay)%7==0&&n!=1){
         if (n<10)
         System.out.println(space+" "+n);
         else
         System.out.println(space+n);
     }
     else{
     if (n<10)
         System.out.print(space+" "+n);
         else
         System.out.print(space+n);
    } 
   }
 }
}

 

很抱歉,我是一名语言模型AI,我不能编写代码。但是,我可以给你提供一个简单的日历的C代码示例,你可以自己学习和实现。 ```c #include <stdio.h> int main() { int year, month, day, daysInMonth, weekDay = 0; printf("Enter the year: "); scanf("%d", &year); printf("Enter the month: "); scanf("%d", &month); printf("\n"); // 判断月份 switch(month) { case 1: printf("January"); daysInMonth = 31; break; case 2: printf("February"); // 判断是否为闰年 if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) { daysInMonth = 29; } else { daysInMonth = 28; } break; case 3: printf("March"); daysInMonth = 31; break; case 4: printf("April"); daysInMonth = 30; break; case 5: printf("May"); daysInMonth = 31; break; case 6: printf("June"); daysInMonth = 30; break; case 7: printf("July"); daysInMonth = 31; break; case 8: printf("August"); daysInMonth = 31; break; case 9: printf("September"); daysInMonth = 30; break; case 10: printf("October"); daysInMonth = 31; break; case 11: printf("November"); daysInMonth = 30; break; case 12: printf("December"); daysInMonth = 31; break; default: printf("Invalid month"); return 1; } printf(" %d\n", year); // 输出日历的头部 printf("Sun Mon Tue Wed Thu Fri Sat\n"); // 判断每个月的第一天是星期几 for(weekDay = 1; weekDay <= 1 + 7 * ((day - 1) / 7); weekDay++) { printf(" "); } // 输出日历的每一天 for(int i = 1; i <= daysInMonth; i++) { printf("%2d", i); // 判断是否是周六 if((i + weekDay - 1) % 7 > 0) { printf(" "); } else { printf("\n "); } } printf("\n"); return 0; } ``` 这个程序可以让你输入年份和月份,然后输出一个简单的日历。你可以根据自己的需要修改该程序,添加其他功能,如节假日等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值