Java练习题 万年历

1 题目要求:

2 代码:

package love;

import java.util.*;

public class demo03 {
	public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		while(scan.hasNext()) {
			int y = scan.nextInt();
			int m = scan.nextInt();
			if(judgeLeapYear(y)) {
				System.out.println("输入的年是闰年");
			}
			else {
				System.out.println("输入的年不是闰年");
			}
			System.out.println("输入的月份的天数是:" + searchMonthDay(y, m));
			int sum1 = 0;
			for(int i = 1900; i < y; i++) {
				sum1 += searchYearDay(i);
			}
			System.out.println("输入的年份距1900年1月1日的总天数是:" + sum1);
			int sum2 = 0;
			for(int i = 1; i < m; i++) {
				sum2 += sear
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是Java实现万年历的代码: ```java import java.util.Scanner; public class Calendar { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入年份:"); int year = scanner.nextInt(); System.out.print("请输入月份:"); int month = scanner.nextInt(); System.out.println("日\t一\t二\t三\t四\t五\t六"); //确定当前月的第一天是星期几 int firstDayOfWeek = getFirstDayOfWeek(year, month); //确定当前月的天数 int totalDays = getTotalDaysOfMonth(year, month); //打印日历 int day = 1; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { if (i == 0 && j < firstDayOfWeek) { System.out.print("\t"); } else if (day > totalDays) { break; } else { System.out.print(day + "\t"); day++; } } System.out.println(); } } //获取当前月的第一天是星期几 public static int getFirstDayOfWeek(int year, int month) { int y = year; int m = month; if (m == 1 || m == 2) { m += 12; y--; } int d = 1; int c = y / 100; y = y % 100; int w = (y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1) % 7; if (w < 0) { w += 7; } return w; } //获取当前月的天数 public static int getTotalDaysOfMonth(int year, int month) { if (month == 2) { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return 29; } else { return 28; } } else if (month == 4 || month == 6 || month == 9 || month == 11) { return 30; } else { return 31; } } } ``` 运行后,会提示输入年份和月份,然后输出该月份的日历。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值