
c#新手一枚 求解 为什么2月天数怎么无法识别 5
intyear,month,days;Console.WriteLine("输入年份");year=Convert.ToInt16(Console.ReadLine())...
int year, month,days;
Console.WriteLine("输入年份");
year = Convert.ToInt16(Console .ReadLine());
Console.WriteLine("输入月份");
month = Convert.ToInt16(Console.ReadLine ());
if (year / 4 == 0 && year / 100 != 0 || year / 400 == 0 || month / 2 == 0)
days = 28;
else if (month / 2 == 0)
days = 29;
else if (month % 2 == 0)
days = 30;
else
days = 31;
Console.WriteLine("输出天数{0}",days );
Console.ReadKey(); 展开
Console.WriteLine("输入年份");
year = Convert.ToInt16(Console .ReadLine());
Console.WriteLine("输入月份");
month = Convert.ToInt16(Console.ReadLine ());
if (year / 4 == 0 && year / 100 != 0 || year / 400 == 0 || month / 2 == 0)
days = 28;
else if (month / 2 == 0)
days = 29;
else if (month % 2 == 0)
days = 30;
else
days = 31;
Console.WriteLine("输出天数{0}",days );
Console.ReadKey(); 展开
1个回答
展开全部
里面的判断逻辑有问题:
第一个if里面需要增加一些括号来保证逻辑关系
month/2==0为啥不直接用 month ==2
month % 2==0不能保证月天数是30天哦,8,10,12月都是31天的
建议用数组先定义好每个月的天数,然后判断是不是闰年决定2月份是否加一天
int[] mDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month == 2 && (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)))
days = mDays[month - 1] + 1;
else
days = mDays[month - 1];
追问
能给我说说,,,我想改正正确的思路么
能给我说说,,,我想改正正确的思路么
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询