菜鸟求大神帮助
import javax.swing.JOptionPane;
public class PC {
public static void main(String[] args){
int option=0;
do{
String yearstring=JOptionPane.showInputDialog("请输入一个年份(如:2010):");
int year=Integer.parseInt(yearstring);
String monthstring=JOptionPane.showInputDialog("请输入一个月份(如:8):");
int month=Integer.parseInt(monthstring);
//JOptionPane.showMessageDialog(null,printmonth(year,month));
System.out.println(printmonth(year,month));
option=JOptionPane.showConfirmDialog(null,"countinue?");
}while(option==JOptionPane.YES_OPTION);
}
static String printmonth(int year,int month){
String printmonth=printmonthtitle(year,month);
printmonth+=printmonthbody(year,month);
return printmonth;
}
static String printmonthtitle(int year,int month){
String printmonthtitle=" "+year+"年"+getmonthname(month)+"\n";
printmonthtitle+="----------------------------------\n";
printmonthtitle+=" SUN MON TUE WED THU FRI SAT \n";
return printmonthtitle;
}
static String getmonthname(int month){
String monthname=null;
switch(month){
case 1:monthname="1月";break;
case 2:monthname="2月";break;
case 3:monthname="3月";break;
case 4:monthname="4月";break;
case 5:monthname="5月";break;
case 6:monthname="6月";break;
case 7:monthname="7月";break;
case 8:monthname="8月";break;
case 9:monthname="9月";break;
case 10:monthname="10月";break;
case 11:monthname="11月";break;
case 12:monthname="12月";
}
return monthname;
}
static String printmonthbody(int year,int month){
int startday=getstarday(year,month);
int numberofdaysinmonth=getnumberofdaysinmonth(year,month);
String printmonthbody="";
for(int i=0;i<startday;i++)printmonthbody+=" ";
for(int i=1;i<=numberofdaysinmonth;i++){
if(i<10)printmonthbody+=" "+i;
else printmonthbody+=" "+i;
if((i+startday)%7==0)printmonthbody+="\n";
}
return printmonthbody;
}
static int getstarday(int year,int month){
int stardayof1970=4;
int totaldays=gettotaldays(year,month);
return (totaldays+stardayof1970)%7;
}
static int gettotaldays(int year,int month){
int total=0;
for(int i=1970;i<year;i++){
if(isleapyear(year))total+=366;
else total+=365;
}
for(int i=1;i<month;i++)total+=getnumberofdaysinmonth(year,month);
return total;
}
static int getnumberofdaysinmonth(int year,int month){
int numberofdaysinmonth;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
numberofdaysinmonth=31;
else if(month==4||month==6||month==9||month==11)
numberofdaysinmonth=30;
else numberofdaysinmonth=(isleapyear(year)?29:28);
return numberofdaysinmonth;
}
static boolean isleapyear(int year){
return year%400==0||(year%4==0&&year%100!=0);
}
}
为什么我的输出结果总是与世界结果不同呢?(已知1970年1月1日为星期四,求其后的任意一年的某月的星期情况并图表打印)
------解决方案--------------------人家原来的代码符号java 命名规范,你改大小写严重影响可读性