编写一程序,实现当输入某年某月某天时,输出是一年的第几天。

如题所述

Java版本

import java.io.*;
public class date{
public static void main(String[] args){
int year,month,date,mark=1,leap=1,sum=0;
System.out.println("请输入年:");
year=IO.getInt();
System.out.println("请输入月:");
month=IO.getInt();
System.out.println("请输入日:");
date=IO.getInt();
if(year<0||year>2050)
{
System.out.println("输入的年 超出范围!\n");
mark=0;
}
if(month<0||month>12)
{
System.out.println("输入的月 超出范围!\n");
mark=0;
}
if(date<0||date>31)
System.out.println("输入的日 超出范围!\n");
else if((year%400==0)||(year%4==0&&year%100!=0))
{
leap=1;
if((date<0||date>29)&&month==2)
{
System.out.println("输入的日 超出范围!\n");
mark=0;
}
}
else
{
leap=0;
if((date<0||date>28)&&month==2)
{
System.out.println("输入的日 超出范围!\n");
mark=0;
}
}
if(mark==1)
{
switch(month)
{
case 1:sum=0;
break;
case 2:sum=31;
break;
case 3:sum=62;
break;
case 4:sum=92;
break;
case 5:sum=123;
break;
case 6:sum=153;
break;
case 7:sum=184;
break;
case 8:sum=215;
break;
case 9:sum=245;
break;
case 10:sum=276;
break;
case 11:sum=306;
break;
case 12:sum=337;
break;
default:
System.out.println("数据错误");
}
if(leap==1&&month>=2)
{
sum++;
sum=sum-2+date;
}
else if(leap==0&&month>=2)
{
sum++;
sum=sum-3+date;
}
else
{
sum++;
sum=sum+date;
}
System.out.println("这一天是这一年的第"+sum+"天");
}
else
System.out.println("输入错误");
}
}

C版本
void main()
{
int year, month, day;
int days;
printf("请输入日期(年 月 日):");

while(1)
{
scanf("%d,%d,%d", &year, &month, &day);
if ((month<13)&&(month>0)&&(day<32)&&(day>0))
break;
system("cls");
printf("输入错误,请重新输入日期(年 月 日):");
}
printf("%d年%d月%d日", year, month, day);
days = sum_day(month, day);
if(leap(year) && month>=3)
days = days + 1;
printf("是该年的第%d天.\n", days);
getch();
}

php版本
class getdate
{
private var $_year;
private var $_dayofyear;
private var $_month;
private var $_day;
private var $mdays = array('31', '28', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31' );
public getdate($year,$dayofyear=0,$month=0,$day=0)
{
$this->_year=$year;
$this->_dayofyear=$dayofyear;
$this->_month=$month;
$this->_day=$day;
if ((($this->_year% 4 == 0 && $this->_year% 100 != 0) || $this->_year% 400 == 0))
$this->mdays[1] = 29;
}
//根据年份,天数求几月几号
public toMonthDay()
{
$days=0;
for ($i = 1; $i <= 12; $i++)
{
$days += $this->mdays[$i - 1];
if ($this->_dayofyear <= $days)
{
$day=$this->mdays[$i-1]-(days - dayofyear);
echo"结果为:".$this->_year."年,".$i."月".$day."日";
break;
}
}
}
//算第几天
public todays()
{
$count=$this->_day;
for($i=0;$i<$this->_month;$i++)
$count+=$this->mdays[$i];
echo"xxx 是这年中的第".$count."天";
}
}
[/php]
用法:
[php]
$mygetdate=new getdate(2008,60);
$mygetdate-> todays();
温馨提示:答案为网友推荐,仅供参考