菜鸟求大神帮助

Ancient__Moon 2011-09-27 11:17:35
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日为星期四,求其后的任意一年的某月的星期情况并图表打印)

...全文
77 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
风尘中国 2011-09-28
  • 打赏
  • 举报
回复
你这个代码严重不符合Java命名规范,我看的太蛋疼了
我想问下你这是要输出相应年月的日历么?
Ancient__Moon 2011-09-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ioe_gaoyong 的回复:]
人家原来的代码符号java 命名规范,你改大小写严重影响可读性

引用 3 楼 ancient__moon 的回复:
引用 1 楼 ioe_gaoyong 的回复:
你这个代码严重不符合Java命名规范,我看的太蛋疼了
我想问下你这是要输出相应年月的日历么?
是啊,就是输出对应年月的日历,我是看书上写的,就改了一点大小写啊。
[/Quote]哦,受教了
风尘中国 2011-09-28
  • 打赏
  • 举报
回复
人家原来的代码符号java 命名规范,你改大小写严重影响可读性
[Quote=引用 3 楼 ancient__moon 的回复:]
引用 1 楼 ioe_gaoyong 的回复:
你这个代码严重不符合Java命名规范,我看的太蛋疼了
我想问下你这是要输出相应年月的日历么?
是啊,就是输出对应年月的日历,我是看书上写的,就改了一点大小写啊。
[/Quote]
Ancient__Moon 2011-09-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mengxiangyue 的回复:]
看的头疼 建议楼主将代码分成几个小的函数那样看着会容易点 也增加了可读性
[/Quote]额,那个不就是一个main方法和其他的几个小的方法么?
Ancient__Moon 2011-09-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ioe_gaoyong 的回复:]
你这个代码严重不符合Java命名规范,我看的太蛋疼了
我想问下你这是要输出相应年月的日历么?
[/Quote] 是啊,就是输出对应年月的日历,我是看书上写的,就改了一点大小写啊。
孟祥月 2011-09-28
  • 打赏
  • 举报
回复
看的头疼 建议楼主将代码分成几个小的函数那样看着会容易点 也增加了可读性
Ancient__Moon 2011-09-28
  • 打赏
  • 举报
回复
谢谢7楼的风尘中国大哥,菜鸟受教了,谢谢!!!
man623188700 2011-09-28
  • 打赏
  • 举报
回复
我恨不写注释的人....
风尘中国 2011-09-28
  • 打赏
  • 举报
回复
你注意结贴,这么低的结帖率CSDN上顾及没人愿意回答你的问题了

你实现的逻辑麻烦,我用Calendar具体实现了一个日历,这样写逻辑会清晰很多,你可以参考一下

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");
}

}

}

}


}


50,550

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧