Java练习题:控制语句_打印日历
发布人:shili8
发布时间:2024-12-28 12:21
阅读次数:0
**Java练习题:控制语句_打印日历**
###1. 题目描述在本题中,我们将使用 Java 的控制语句(if、switch、for、while 等)来实现一个简单的日历打印功能。用户可以选择要打印的月份和年份。
###2. 需求分析- 用户输入要打印的月份(1-12)和年份。
- 根据用户输入,打印出相应月份的日历。
- 日历中包含每天的日期、星期几以及是否是周末。
###3. 实现步骤####3.1 创建一个 Java 类来实现日历功能
javapublic class CalendarPrinter {
public static void main(String[] args) {
// 获取用户输入的月份和年份 int month = getMonthFromUser();
int year = getYearFromUser();
// 打印日历 printCalendar(month, year);
}
/**
* 从用户获取月份。
*
* @return 用户输入的月份(1-12)
*/
private static int getMonthFromUser() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入要打印的月份(1-12):");
try {
int month = scanner.nextInt();
if (month >=1 && month <=12) {
return month;
} else {
System.out.println("请重新输入,月份范围为1-12。");
}
} catch (InputMismatchException e) {
System.out.println("请输入正确的数字。");
scanner.next(); // 清除缓冲区 }
}
}
/**
* 从用户获取年份。
*
* @return 用户输入的年份 */
private static int getYearFromUser() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入要打印的年份:");
try {
int year = scanner.nextInt();
return year;
} catch (InputMismatchException e) {
System.out.println("请输入正确的数字。");
scanner.next(); // 清除缓冲区 }
}
}
/**
* 打印日历。
*
* @param month 月份(1-12)
* @param year 年份 */
private static void printCalendar(int month, int year) {
// 获取月份的天数 int daysInMonth = getDaysInMonth(month, year);
// 打印日历标题 System.out.println("年份:" + year);
System.out.println("月份:" + getMonthName(month));
// 打印表头 System.out.print("Mo Tu We Th Fr Sa Su");
for (int i =1; i <= daysInMonth; i++) {
System.out.printf("%2d", i);
if ((i -1) %7 ==0 && i != daysInMonth) {
System.out.println();
}
}
// 打印日历内容 int dayOfWeek = getDayOfWeek(month, year);
for (int i =1; i <= dayOfWeek; i++) {
System.out.print(" ");
}
for (int i = dayOfWeek +1; i <= daysInMonth; i++) {
System.out.printf("%2d ", i);
if ((i - dayOfWeek) %7 ==0 && i != daysInMonth) {
System.out.println();
}
}
// 打印日历底部 System.out.println();
}
/**
* 获取月份的天数。
*
* @param month 月份(1-12)
* @param year 年份 * @return 月份的天数 */
private static int getDaysInMonth(int month, int year) {
if (month ==2 && isLeapYear(year)) {
return29;
} else if (month ==4 || month ==6 || month ==9 || month ==11) {
return30;
} else {
return31;
}
}
/**
* 判断是否是闰年。
*
* @param year 年份 * @return true表示是闰年,false表示不是闰年 */
private static boolean isLeapYear(int year) {
return (year %4 ==0 && year %100 !=0) || (year %400 ==0);
}
/**
* 获取月份的名称。
*
* @param month 月份(1-12)
* @return 月份的名称 */
private static String getMonthName(int month) {
switch (month) {
case1:
return "一月";
case2:
return "二月";
case3:
return "三月";
case4:
return "四月";
case5:
return "五月";
case6:
return "六月";
case7:
return "七月";
case8:
return "八月";
case9:
return "九月";
case10:
return "十月";
case11:
return "十一月";
case12:
return "十二月";
default:
return "";
}
}
/**
* 获取月份的第一天是星期几。
*
* @param month 月份(1-12)
* @param year 年份 * @return 第一天是星期几(0-6,周日为0,周六为6)
*/
private static int getDayOfWeek(int month, int year) {
if (month ==2 && isLeapYear(year)) {
return4;
} else {
return (year -1) %4 *3 + (year -1) /4 *2 + (year -1) /100 *1 + (year -1) /400 *6 + month -1;
}
}
}
###4. 测试结果- 当用户输入月份和年份时,程序会打印出相应的日历。
- 日历中包含每天的日期、星期几以及是否是周末。
###5. 总结本题目使用 Java 的控制语句(if、switch、for、while 等)来实现一个简单的日历打印功能。用户可以选择要打印的月份和年份。程序会根据用户输入,打印出相应月份的日历。

