package com.mysoft.test;import java.util.*;
public class Calendar{
private static Object DAY_OF_WEEK;
public void printCalcn(int year,int month)
{
Calendar myca=Calendar.getInstance();
myca.set(year,month-1,1);
int week=myca.get(Calendar.DAY_OF_WEEK);
int days=this.getDaysOfMonths(year,month);
int w=0;
System.out.println(" 日 一 二 三 四 五 六 ");
for(int j=1;j<=days;j++)
{
if(j==1)
for(int i=1;i<week;i++)
{
System.out.print(" ");
w++;
}
if(j<10){
System.out.print(" "+j);
}
else{
System.out.print(" "+j);
}
w++;
if(w%7==0)
System.out.println();
}
}
private int getDaysOfMonths(int year, int month) {
int days=0;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 4:
case 6:
case 9:
case 11:
days=30;
break;
case 2:
if((year%4==0)||(year%400==0))
days=29;
else
days=28;
}
return days;
}
public static void main(String[] args) {
Calendar Cal1=new Calendar();
Cal1.printCalcn(2012, 10);
}}这个程序怎么改??
public class Calendar{
private static Object DAY_OF_WEEK;
public void printCalcn(int year,int month)
{
Calendar myca=Calendar.getInstance();
myca.set(year,month-1,1);
int week=myca.get(Calendar.DAY_OF_WEEK);
int days=this.getDaysOfMonths(year,month);
int w=0;
System.out.println(" 日 一 二 三 四 五 六 ");
for(int j=1;j<=days;j++)
{
if(j==1)
for(int i=1;i<week;i++)
{
System.out.print(" ");
w++;
}
if(j<10){
System.out.print(" "+j);
}
else{
System.out.print(" "+j);
}
w++;
if(w%7==0)
System.out.println();
}
}
private int getDaysOfMonths(int year, int month) {
int days=0;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 4:
case 6:
case 9:
case 11:
days=30;
break;
case 2:
if((year%4==0)||(year%400==0))
days=29;
else
days=28;
}
return days;
}
public static void main(String[] args) {
Calendar Cal1=new Calendar();
Cal1.printCalcn(2012, 10);
}}这个程序怎么改??
case 2:
if((year%4==0)||(year%400==0))
days=29;
else
days=28;
}
这块代码的逻辑肯定是不对的,后面(year%400==0)是不可能执行的,最关键的是,你的闰年规则没弄清楚。
闰年规则应该是这样:
if(year%400==0||(year%4==0&&year%100!=0))
days = 29;
else
days = 28;
public void myCalendar()
{
int year = (int)(Math.random()* 1000)+2000;
Calendar calendar = Calendar.getInstance();
//calendar.set(year, 2, 1);
int day = 28;
boolean isLeap = judge(year);
int week = calendar.get(calendar.DAY_OF_WEEK) - 1;
System.out.println(year + "年2月的日历页:");
System.out.println("日 一 二 三 四 五 六");
if(isLeap)
day = 29;
String a[] = new String[week + day];
for(int x = 0; x < week; x++)
{
a[x] = " ";
}
for(int i = week, n = 1; i < a.length; i++)
{
if(n <= 9)
a[i] = String.valueOf(n) + " ";
else
a[i] = String.valueOf(n);
n++;
}
for(int j = 0; j < a.length; j++)
{
if(j % 7 == 0)
{
System.out.println();
}
System.out.print(" " + a[j]);
}
}
public static boolean judge(int year)
{
boolean yearleap = (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0);//采用布尔数据计算判断是否能整除
return yearleap;
}
} package com.fzw.test.graph;public class Test {
public static void main(String[] args) {
Year year = new Year();
year.myCalendar();
}
}