万年历方法调用练习

方法区

package com.calendar;
public class Calendar {
	int year;//定义年份
	int month;//定义月份
	/*求年份总天数(1990-输入年数)*/
	int yearSum = 0;
	public int getyearSum() {
		for(int i=1900;i<year;i++) {
			if((i%100!=0&&i%4==0)||(i%100==0&&i%400==0)) {
				yearSum+=366;
			}else {
				yearSum+=365;
			}
		}
		return yearSum;	
	}
	/*求月份总天数*/
	int monthSum = 0;
	public int getmonthSum() {
		for(int i=1;i<month;i++) {
			monthSum+=getday(year,i);
		}
		return monthSum;
	}
	/*求每月第一天是星期几*/
	int week = 0;
	public int getweek() {
		week = (yearSum+monthSum+1)%7;
		return week;
	}
	/*求每月有多少天*/
	int day = 0;
	public int  getday(int year,int month) {
			switch(month) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				day =31;
				return day;
			case 2:
				if(isleapYear()==true) {
					day= 29;
					return day;
				}else {
					day= 28;
					return day;
				}
			default:
				day= 30;
				return day;
		}
	}
	/*判断是否是闰年*/
	boolean leapYear;
	public boolean isleapYear() {
		if((year%100!=0&&year%4==0)||(year%100==0&&year%400==0)) {
			leapYear = true;
		}else {
			leapYear = false;
		}
		return leapYear;
	}
}

调用区

package com.calendar;
import java.util.Scanner;
public class CalendarDemo {
	public static void main(String[] args) {
		Calendar c = new Calendar();
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入年份");
		c.year= sc.nextInt();
		System.out.println("请输入月份");
		c.month = sc.nextInt();
		c.getyearSum();
		c.getmonthSum();
		c.getweek();
		c.getday(c.year,c.month);
		c.isleapYear();
		System.out.println(c.yearSum);
		System.out.println(c.monthSum);
		System.out.println(c.day);
		System.out.println(c.year+"年"+c.month+"月");
		System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
		int count = 0;
		for(int i=1;i<=c.week;i++) {
			System.out.print("\t");
			count++;
		}
		for(int a=1;a<=c.day;a++) {
			if((a+count)%7==0) {
				System.out.print(a+"\t");
				System.out.println();
			}else {
				System.out.print(a+"\t");
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值