Java作业ch7_1(刷分篇)

package homework05;
/*
* 题目:编写一个应用程序,输出某年某月的日历页,通过main()方法的参数将年和月份时间传递到程序中。
*/

import java.util.Calendar;

class CalendarDisplay{
private int year;
private int month;
public CalendarDisplay(int year,int month){
this.year=year;
this.month=month-1;
}

//返回当前月份的总天数。
public int getNumberOfDaysInMonth(int year,int month){
int num=0;
if(month == 1|| month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
num = 31;
else if(month == 4 || month == 6 || month == 9|| month == 11)
num = 30;
else if(month == 2){
if(year%4==0&&year%100!=0||year%400==0)
num = 29;
else
num = 28;
}
return num;
}

public void display(){
Calendar cal=Calendar.getInstance();
cal.set(year, month, 1);
int weekOf=cal.get(Calendar.DAY_OF_WEEK)-1;
month++;
String a[]=new String[weekOf+getNumberOfDaysInMonth(year,month)];
for(int i=0;i<weekOf;i++){
a[i]="";
}
for(int i=weekOf,n=1;i<weekOf+getNumberOfDaysInMonth(year,month);i++){
a[i]=String.valueOf(n);
n++;
}
System.out.println(" 这是"+year+"年"+month+"月的日历页!");
System.out.printf("%10c%10c%10c%10c%9c%8c%10c/n",'日','一','二','三','四','五','六');
for(int i=0;i<a.length;i++){
if(i%7==0&&i!=0)
System.out.println("/n");
System.out.printf("%5s",a[i]);
}
}
}

public class ch07_1 {
public static void main(String[] args) {
new CalendarDisplay(Integer.parseInt(args[0]),Integer.parseInt(args[1])).display();
}

}

你可能感兴趣的:(java)