| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 520 人关注过本帖
标题:这是算法问题吗
只看楼主 加入收藏
laishang
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2006-3-2
收藏
 问题点数:0 回复次数:3 
这是算法问题吗
这个程序语法上好象没什么错误,但输出的结果却有写错误。比如输入2001年1月1日
输出结果是:昨天 2001年12月0日,明天 2001年2月1日,大家看看是哪里错了;

public class Time {
int day,yesterday,tomorrow,month,lastmonth;
int nextmonth,year,lastyear,nextyear;
boolean leap;
Time(int y,int m,int d){
year=y;
month=m;
day=d;


}
void leapYear(){
if((year%400==0)|(year%100!=0)&(year%4==0))
leap=false;
}
public void shijian(){
switch(month)
{
case 1:
if(day>1&&day<31)
lastmonth=month;
yesterday=day-1;
lastyear=year;
nextmonth=month;
tomorrow=day+1;
nextyear=year;
if(day==1)
lastmonth=12;
yesterday=31;
lastyear=year-1;
nextmonth=month;
tomorrow=day+1;
nextyear=year;
if(day==31)
lastmonth=month;
yesterday=day-1;
lastyear=year;
nextmonth=month+1;
tomorrow=1;
nextyear=year;
break;
case 4:
case 6:
case 8:
case 9:
case 11:

if(day>1&&day<30)
lastmonth=month;yesterday=day-1;lastyear=year;
nextmonth=month;tomorrow=day+1;nextyear=year;
if(day==1)
lastmonth=month-1;yesterday=31;lastyear=year;
nextmonth=month;tomorrow=day+1;nextyear=year;
if(day==30)
lastmonth=month;yesterday=day-1;lastyear=year;
nextmonth=month+1;tomorrow=1;nextyear=year;
break;
case 5:
case 7:
case 10:
if(day>1&&day<31)
lastmonth=month;yesterday=day-1;lastyear=year;
nextmonth=month;tomorrow=day+1;nextyear=year;
if(day==1)
lastmonth=month-1;yesterday=30;lastyear=year;
nextmonth=month;tomorrow=day+1;nextyear=year;
if(day==31)
lastmonth=month;yesterday=day-1;lastyear=year;
nextmonth=month+1;tomorrow=1;nextyear=year;
break;
case 12:
if(day>1&&day<31)
lastmonth=month;yesterday=day-1;lastyear=year;
nextmonth=month;tomorrow=day+1;nextyear=year;
if(day==1)
lastmonth=month-1;yesterday=30;lastyear=year;
nextmonth=month;tomorrow=day+1;nextyear=year;
if(day==31)
lastmonth=month;yesterday=day-1;lastyear=year;
nextmonth=1;tomorrow=1;nextyear=year+1;
break;
case 2:
if(leap){

if(day>1&&day<29)
lastmonth=month;yesterday=day-1;lastyear=year;
nextmonth=month;tomorrow=day+1;nextyear=year;
if(day==1)
lastmonth=month-1;yesterday=31;lastyear=year;
nextmonth=month;tomorrow=day+1;nextyear=year;
if(day==29)
lastmonth=month;yesterday=day-1;lastyear=year;
nextmonth=month+1;tomorrow=1;nextyear=year;
}
else
{
if(day>1&&day<28)
lastmonth=month;yesterday=day-1;lastyear=year;
nextmonth=month;tomorrow=day+1;nextyear=year;
if(day==1)
lastmonth=month-1;yesterday=31;lastyear=year;
nextmonth=month;tomorrow=day+1;nextyear=year;
if(day==28)
lastmonth=month;yesterday=day-1;lastyear=year;
nextmonth=month+1;tomorrow=1;nextyear=year;
}
break;
case 3:
if(day>1&&day<31)
lastmonth=month;yesterday=day-1;lastyear=year;
nextmonth=month;tomorrow=day+1;nextyear=year;
if(day==1)
if(leap){
lastmonth=month-1;yesterday=29;lastyear=year;
nextmonth=month;tomorrow=day+1;nextyear=year;
}
else{
lastmonth=month-1;yesterday=28;lastyear=year;
nextmonth=month;tomorrow=day+1;nextyear=year;
}
if(day==31)
lastmonth=month;yesterday=day-1;lastyear=year;
nextmonth=month+1;tomorrow=1;nextyear=year;
}
}
public void print(){
System.out.println("今天 "+year+"年"+month+"月"+day+"日");
System.out.println("昨天 "+lastyear+"年"+lastmonth+"月"+yesterday+"日");
System.out.println("明天 "+nextyear+"年"+nextmonth+"月"+tomorrow+"日");
}
public static void main(String[] args) {

Time a=new Time(2001,1,1);
a.leapYear();
a.shijian();
a.print();
}
}
搜索更多相关主题的帖子: 算法 
2006-03-22 13:40
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
你要弄这么复杂干嘛啊,用Calendar类不就什么问题都解决了吗?

可惜不是你,陪我到最后
2006-03-22 14:40
laishang
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2006-3-2
收藏
得分:0 

呵呵 我不太会用那个所以就只好这么做了,不过我已经改过来了,原来那几个if语句没用花括号括起来,加上就没事了

2006-03-22 16:17
飘飘叶子
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:34
帖 子:597
专家分:10
注 册:2005-8-17
收藏
得分:0 
如果你要做关于时间的操作,就去查api帮助,那样会好一些
如果要你算1000的阶乘你不是要写几千行了?

向着软件工程师的目标前进!
2006-03-22 22:07
快速回复:这是算法问题吗
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016721 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved