6-6 打印一个月的日历(日历) (30分) java pta

该博客介绍如何使用Java编程实现一个方法,根据用户输入的年份和月份打印出相应月份的日历。内容包括函数接口定义,裁判测试程序样例,输入输出样例,以及具体的代码实现。示例中展示了如何处理1号前的空档和打印具体日期,并提供了测试案例以验证代码的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本题要求实现一个方法,用户输入年代和月份,方法输出当月日历。
注:相关打印设置为:System.out.print(" “); //打印1号前的空档,空1天打印1次;
System.out.printf(”%4d",i); //打印具体日期;
一个月打印完了以后换行。

函数接口定义:

详见主方法中的调用。

裁判测试程序样例:

在这里给出函数被调用进行测试的例子。例如:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year=input.nextInt();
int month = input.nextInt();
printMonth(year, month);
input.close();
}
public static void printMonth(int year, int month){
printMonthTitle(year, month);
printMonthBody(year, month);
}
public static void printMonthTitle(int year, int month){
System.out.println(" “+getMonthName(month)+ " " + year);
System.out.println(”-----------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
/* 请在这里填写答案 */
}

输入样例:

在这里给出一组输入。例如:

2020
9

输出样例:

在这里给出相应的输出。例如:

September  2020
-----------------------------
Sun Mon Tue Wed Thu Fri Sat
          1   2   3   4   5
  6   7   8   9  10  11  12
 13  14  15  16  17  18  19
 20  21  22  23  24  25  26
 27  28  29  30
static void printMonthBody(int year,int month) {
   
			int count=0;
			int first=getStartDay(year,month);
			for(int i=1;i<=first;++i) {
   
				System.out.print("    ");
			}
			count+=<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值