java生成工作日历算法_试题 算法提高 任意年月日历输出(Java)

试题 算法提高 任意年月日历输出(Java)

试题 算法提高 任意年月日历输出(Java)

98217165571d57bbcd6275c1512f53da.png

1cd00fb02f038c25f25f0c694501ba97.png

import java.util.Calendar;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int year = sc.nextInt();

int month = sc.nextInt();

sc.close();

Calendar cal = Calendar.getInstance();

cal.set(year, month - 1, 1);

int week = cal.get(Calendar.DAY_OF_WEEK);//一号是星期几

System.out.println("Calendar " + year + "-" + (month >= 10 ? month : "0" + month));

System.out.println("---------------------");

System.out.println(" Su Mo Tu We Th Fr Sa");

System.out.println("---------------------");

int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);//这个月天数

//第一行日期

for (int i = 2 - week, j = 0; i <= days; ++i, ++j) {

if (j == (7 - week + 1)) {

break;

}

if (i <= 0) {

System.out.print(" ");

j--;

continue;

}

System.out.printf(" %2d", i);

}

//后面n行日期

for (int i = (7 - week + 1) + 1, j = 0; i <= days; ++i, ++j) {

if (j % 7 == 0) {

System.out.println();

}

System.out.printf(" %2d", i);

}

System.out.println();

System.out.println("---------------------");

}

}

试题 算法提高 任意年月日历输出(Java)相关教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值