这是算法问题吗
这个程序语法上好象没什么错误,但输出的结果却有写错误。比如输入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();
}
}
搜索更多相关的解决方案:
算法
----------------解决方案--------------------------------------------------------
你要弄这么复杂干嘛啊,用Calendar类不就什么问题都解决了吗?
----------------解决方案--------------------------------------------------------
呵呵 我不太会用那个所以就只好这么做了,不过我已经改过来了,原来那几个if语句没用花括号括起来,加上就没事了
----------------解决方案--------------------------------------------------------
如果你要做关于时间的操作,就去查api帮助,那样会好一些
如果要你算1000的阶乘你不是要写几千行了?
----------------解决方案--------------------------------------------------------