using System;
class YearDay
{
static void Main()
{
start:
Console.WriteLine("这个程序可以计算出今年已经过了多少天!");
Console.WriteLine("请输入年份");
int year=Convert.ToInt32(Console.ReadLine());
int total=365;
if(year%400==0||year%4==0&&(year%100)!=0)
{
total=366;
}
int y=total;
Console.WriteLine("请输入当前的月份:");//输入月
int month=Convert.ToInt32(Console.ReadLine());//System.Convert.ToInt32,从输入设备读取32位类型整型数据;
Console.WriteLine("请输入本月的几号:");//输入日
int day=Convert.ToInt32(Console.ReadLine());
if((month>0)&&(month<13))
{
switch(month)
{
case 1:
{
total-=31;
if((day>0)&&(day<32))
goto case 2;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 2:
{
total-=28;
if ((day > 0) && (day < 29))
{
Console.WriteLine("今年是平年,有365天");
goto case 3;
}
else if ((y == 366) && (day > 0) && (day < 30))
{
Console.WriteLine("今年是闰年,有366天");
goto case 3;
}
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 3:
{
total-=31;
if((day>0)&&(day<32))
goto case 4;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 4:
{
total-=30;
if((day>0)&&(day<31))
goto case 5;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 5:
{
total-=31;
if((day>0)&&(day<32))
goto case 6;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 6:
{
total-=30;
if((day>0)&&(day<31))
goto case 7;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 7:
{
total-=31;
if((day>0)&&(day<32))
goto case 8;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 8:
{
total-=31;
if((day>0)&&(day<32))
goto case 9;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 9:
{
total-=30;
if((day>0)&&(day<31))
goto case 10;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 10:
{
total-=31;
if((day>0)&&(day<32))
goto case 11;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 11:
{
total-=30;
if((day>0)&&(day<31))
goto case 12;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 12:
{
total-=31;
if((day>0)&&(day<32))
goto default;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
default:total+=day;break;
}
Console.WriteLine("今年已经过了{0}天!",total);
}
else
{
Console.WriteLine("您输入的当前月份无效!请重新输入");
goto start;
}
}
}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
这个程序编译没有问题,但我在调试程序时输入月份为1,3等31天的月份,再输入日期为31,30就会判断错误,请帮忙看下,为什么?谢谢!
class YearDay
{
static void Main()
{
start:
Console.WriteLine("这个程序可以计算出今年已经过了多少天!");
Console.WriteLine("请输入年份");
int year=Convert.ToInt32(Console.ReadLine());
int total=365;
if(year%400==0||year%4==0&&(year%100)!=0)
{
total=366;
}
int y=total;
Console.WriteLine("请输入当前的月份:");//输入月
int month=Convert.ToInt32(Console.ReadLine());//System.Convert.ToInt32,从输入设备读取32位类型整型数据;
Console.WriteLine("请输入本月的几号:");//输入日
int day=Convert.ToInt32(Console.ReadLine());
if((month>0)&&(month<13))
{
switch(month)
{
case 1:
{
total-=31;
if((day>0)&&(day<32))
goto case 2;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 2:
{
total-=28;
if ((day > 0) && (day < 29))
{
Console.WriteLine("今年是平年,有365天");
goto case 3;
}
else if ((y == 366) && (day > 0) && (day < 30))
{
Console.WriteLine("今年是闰年,有366天");
goto case 3;
}
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 3:
{
total-=31;
if((day>0)&&(day<32))
goto case 4;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 4:
{
total-=30;
if((day>0)&&(day<31))
goto case 5;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 5:
{
total-=31;
if((day>0)&&(day<32))
goto case 6;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 6:
{
total-=30;
if((day>0)&&(day<31))
goto case 7;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 7:
{
total-=31;
if((day>0)&&(day<32))
goto case 8;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 8:
{
total-=31;
if((day>0)&&(day<32))
goto case 9;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 9:
{
total-=30;
if((day>0)&&(day<31))
goto case 10;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 10:
{
total-=31;
if((day>0)&&(day<32))
goto case 11;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 11:
{
total-=30;
if((day>0)&&(day<31))
goto case 12;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
case 12:
{
total-=31;
if((day>0)&&(day<32))
goto default;
else
{
Console.WriteLine("您输入的当前日期无效!请重新输入");
goto start;
}
}
default:total+=day;break;
}
Console.WriteLine("今年已经过了{0}天!",total);
}
else
{
Console.WriteLine("您输入的当前月份无效!请重新输入");
goto start;
}
}
}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
这个程序编译没有问题,但我在调试程序时输入月份为1,3等31天的月份,再输入日期为31,30就会判断错误,请帮忙看下,为什么?谢谢!
using System;
class YearDay
{
static void Main()
{
start:
Console.WriteLine( "这个程序可以计算出今年已经过了多少天! ");
Console.WriteLine( "请输入年份 ");
int year=Convert.ToInt32(Console.ReadLine());
int total=365;
if(year%400==0 || year%4==0&&(year%100)!=0)
{
total=366;
}
int y=total;
Console.WriteLine( "请输入当前的月份: ");//输入月
int month=Convert.ToInt32(Console.ReadLine());//System.Convert.ToInt32,从输入设备读取32位类型整型数据;
Console.WriteLine( "请输入本月的几号: ");//输入日
int day=Convert.ToInt32(Console.ReadLine());
if((month> 0)&&(month <13))
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if((day> 0)&&(day <32))
{
break;
}
else
{
Console.WriteLine( "您输入的当前日期无效!请重新输入 ");
goto start;
}
case 2:
if ((day > 0) && (day < 29))
{
Console.WriteLine( "今年是平年,有365天 "); break;
}
else if ((y == 366) && (day > 0) && (day < 30))
{
Console.WriteLine( "今年是闰年,有366天 ");
break;
}
else
{
Console.WriteLine( "您输入的当前日期无效!请重新输入 ");
goto start;
}
case 4:
case 6:
case 9:
case 11:
if((day> 0)&&(day <31))
{break;}
else
{
Console.WriteLine( "您输入的当前日期无效!请重新输入 ");
goto start;
}
} DateTime dt = new DateTime(year,month,day); Console.WriteLine( "今年已经过了{0}天! ",dt.DayOfYear-1);
Console.Read();
}
else
{
Console.WriteLine( "您输入的当前月份无效!请重新输入 ");
goto start;
}
}
}