循环打印日历

输入年份和代表该年第一天是星期几的数字,打印该年每个月的日历
代码:
package com.im;

import java.util.Scanner;

public class dd {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner input = new Scanner(System.in);
    // Prompt the user to enter input
    System.out.print("Enter a year: ");
    int year = input.nextInt();

    System.out.print("Enter the first day of the year: ");
    int firstDay = input.nextInt();

    int startDay = firstDay;
    int numberOfDaysInMonth = 0;

    // Display calendar for each month
    for (int month = 1; month <= 12; month++) {
      // Display Calendar title
      System.out.print("          ");
      switch (month) {
      case 1:
        System.out.println("January " + year);
        numberOfDaysInMonth = 31;
        break;
      case 2:
        System.out.println("Feburary " + year);
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
          numberOfDaysInMonth = 29;
        else
          numberOfDaysInMonth = 28;
        break;
      case 3:
        System.out.println("March " + year);
        numberOfDaysInMonth = 31;
        break;
      case 4:
        System.out.println("April " + year);
        numberOfDaysInMonth = 30;
        break;
      case 5:
        System.out.println("May " + year);
        numberOfDaysInMonth = 31;
        break;
      case 6:
        System.out.println("June " + year);
        numberOfDaysInMonth = 30;
        break;
      case 7:
        System.out.println("July " + year);
        numberOfDaysInMonth = 31;
        break;
      case 8:
        System.out.println("August " + year);
        numberOfDaysInMonth = 31;
        break;
      case 9:
        System.out.println("September " + year);
        numberOfDaysInMonth = 30;
        break;
      case 10:
        System.out.println("October " + year);
        numberOfDaysInMonth = 31;
        break;
      case 11:
        System.out.println("November " + year);
        numberOfDaysInMonth = 30;
        break;
      case 12:
        System.out.println("December " + year);
        numberOfDaysInMonth = 31;
        break;
      }

      System.out.println("-----------------------------");
      System.out.println(" Sun Mon Tue Wed Thu Fri Sat");

      // Pad space before the first day of the month
      int i = 0;
      for (i = 0; i < startDay; i++)
        System.out.print("    ");

      for (i = 1; i <= numberOfDaysInMonth; i++) {
        if (i < 10)
          System.out.print("   " + i);
        else
          System.out.print("  " + i);

        if ((i + startDay) % 7 == 0)
          System.out.println();
      }

      System.out.println();
      System.out.println();

      // Get the start day for the next month
      startDay = (startDay + numberOfDaysInMonth) % 7;
    }
}

}
这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值