输出某一年的各个月份的天数
📆Date: 2023年1月8日
✒️Author: SmartBoy
📓Classify:蓝桥杯试题集
🔖Language: Java
✨题目描述:
输出某一年的各个月份的天数
输入任意年份,输出该年份各月天数(请使用 Calendar 类中的方法)
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
输入描述:
任意年份(int类型整数)
输出描述:
x年1月:yy天 x年2月:yy天 x年3月:yy天 x年4月:yy天 x年5月:yy天 x年6月:yy天 x年7月:yy天 x年8月:yy天 x年9月:yy天 x年10月:yy天 x年11月:yy天 x年12月:yy天(x为输入的任意年份,yy为当月天数)
示例1
输入例子:
2021
输出例子:
2021年1月:31天 2021年2月:28天 2021年3月:31天 2021年4月:30天 2021年5月:31天 2021年6月:30天 2021年7月:31天 2021年8月:31天 2021年9月:30天 2021年10月:31天 2021年11月:30天 2021年12月:31天
🎉解题思路:
利用java的API进行解题
🥇Java源码:
import java.util.Calendar; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner console = new Scanner(System.in); int year = console.nextInt(); //write your code here...... Calendar calendar = Calendar.getInstance(); for (int i = 1; i <= 12 ; i++) { calendar.set(year,i,0); System.out.println(year+"年"+i+"月:"+calendar.get(calendar.DAY_OF_MONTH)+"天"); } } }
🍖附录
输入年月日,计算这一年的总天数,并输出这一年的每个月的天数。
import java.util.Calendar;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Calendar calendar = Calendar.getInstance();
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
int currentDay = 0;
int totalDays = 0;
System.out.println("你输入的是:"+year+"年"+month+"月"+day+"日");
for (int i = 1; i <= 12 ; i++) {
calendar.set(year,i,0);
System.out.println(year+"年"+i+"月:"+calendar.get(Calendar.DAY_OF_MONTH)+"天");
totalDays += calendar.get(Calendar.DAY_OF_MONTH);
}
System.out.println(year+"年总共有"+totalDays+"天");
if (month >= 1 && month <= 12) {
for (int i = 1; i < month; i++) {
calendar.set(year,i,0);
currentDay += calendar.get(Calendar.DATE);
}
currentDay += day;
System.out.println(year+"年"+month+"月"+day+"日是该年的第"+currentDay+"天");
}
else {
System.out.println("请输入正确年月日");
}
}
}