菜鸟求大神帮助
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 命名规范,你改大小写严重影响可读性
import javax.swing.*;import java.util.Calendar;/** * Created by IntelliJ IDEA. * User: ioe_gaoyong * Date: 2011-9-28 * Time: 0:33:20 * To change this template use File | Settings | File Templates. * 这个类里面要自己设计排布的方法,自己画出一个日历 */public class PC2 { 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(year+"年"+month+"月");// printCalendar(2011,9); printCalendar(year,month); option=JOptionPane.showConfirmDialog(null,"countinue?"); }while(option==JOptionPane.YES_OPTION); } public static void printCalendar(int year,int month){ String printMonthTitle=" "+year+"年"+month+"月"+"\n"; printMonthTitle+="----------------------------------\n"; printMonthTitle+="SUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT \n"; System.out.println(printMonthTitle); Calendar c=Calendar.getInstance(); c.set(year,month-1,1);//现在的时间是该月的第一天 int firstDayOfWeek=c.get(Calendar.DAY_OF_WEEK);//这个月的1号是星期几 int sumDays=c.getMaximum(Calendar.DAY_OF_MONTH);//获得这个月总共多少天// System.out.println(firstDayOfWeek); for(int i=1;i<firstDayOfWeek;i++){ System.out.print(" \t"); } if(firstDayOfWeek!=7){ System.out.print(1+" \t"); }else{ System.out.println(1+" \t"); } for(int i=2;i<=sumDays;i++){ c.add(Calendar.DAY_OF_MONTH,1); if(c.get(Calendar.DAY_OF_WEEK)==7){//如果是星期六 System.out.println(i); }else{ if(i<=8){ System.out.print(i+" \t"); }else{ System.out.print(i+" \t"); } } } } }