用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

基础 日历 可实现切换到上、下月份的功能

2019-03-02 作者:阳光彩虹小白驴举报

[java]代码库

import java.util.Scanner;

public class code1 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("请输入你想查询的年份(至少为1900年):");
		int year = in.nextInt();
		System.out.println("请输入你想查询的月份:");
		int month = in.nextInt();
		int flag = 0;
		while (flag == 0) {
			int sum = 0;
			int num = 0;
			for (int date = 1900; date < year; date++) {
				if (date % 4 == 0 && date % 100 != 0 || date % 400 == 0) {
					sum = sum + 366;
				} else {
					sum = sum + 365;
				}
			}
			for (int date1 = 1; date1 < month; date1++) {
				if (date1 == 1 || date1 == 3 || date1 == 5 || date1 == 7 || date1 == 8 || date1 == 10 || date1 == 12) {
					sum = sum + 31;
				} else if (date1 == 2) {
					if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
						sum = sum + 29;
					} else {
						sum = sum + 28;
					}
				} else {
					sum = sum + 30;
				}
			}
			sum = sum + 1;
			System.out.println("**************************");
			System.out.println("\t下面是"+year+"年"+month+"月"+"的日历");
			System.out.println("**************************");
			System.out.println("日\t一\t二\t三\t四\t五\t六");
			for (int i = 0; i < (sum % 7); i++) {
				System.out.print(" \t");
			}
			if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 | month == 12) {
				num = 31;
			} else if (month == 2) {
				if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
					num = 29;
				} else {
					num = 28;
				}
			} else {
				num = 30;
			}

			for (int i = 1; i <= num; i++, sum++) {
				System.out.print(i);
				if (sum % 7 == 6) {
					System.out.println();
				} else {
					System.out.print("\t");
				}
			}
			System.out.println();
			System.out.println("请选择1、2、3");
			System.out.println("1、观看上一个月的日历");
			System.out.println("2、观看下一个月的日历");
			System.out.println("3、结束");
			int select = in.nextInt();
			if (select == 1) {
				month = month - 1;
				if (month == 0) {
					year = year - 1;
					month = 12;
				}
				flag = 0;
			} else if (select == 2) {
				month = month + 1;
				if (month == 13) {
					year = year + 1;
					month = 1;
				}
				flag = 0;
			} else {
				flag = 1;
			}
		}
	}
}


分享到:
更多

网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。