用C#编写一个输入某一年的年份和月份,计算该月的天数的编程.
答案:6 悬赏:20 手机版
解决时间 2021-03-17 09:00
- 提问者网友:趣果有间
- 2021-03-16 13:41
提示:(1)一年中的大月(1月、3月、5月、7月、8月、10月、12月),每月的天数为31天;(2)一年中的小月(4月、6月、9月、11月),每月的天数为30天;(3)对于2月,则要判断该年是平年还是闰年,平年的2月为28天,闰年的2月为29天。某年符合下面两个条件之一就是闰年:①年份能被400整除;②年份能被4整除,但不能被100整除。<我学这个没多久,希望不要太难理解,谢谢勒>
最佳答案
- 五星知识达人网友:夜风逐马
- 2021-03-16 14:09
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Maker
{
class Program
{
static void Main(string[] args)
{
int a = 31;
int b = 30;
int c, mouth=0, day=0,year;
int sum=0;
do
{
Console.WriteLine("请输入年:");
year=int.Parse(Console.ReadLine());
Console.WriteLine("请输入月份:");
mouth = int.Parse(Console.ReadLine());
Console.WriteLine("请输入日数:");
day = int.Parse(Console.ReadLine());
if ((mouth < 1 || mouth > 12) || (day < 1 || day > 31)||year<=0)
{
Console.WriteLine("输入有误!");
}else
{
if(year%4!=0)
{
c = 28;
if (mouth <= 2)
{
sum = (mouth - 1) * 31 + day;
Console.WriteLine(year + "年" + mouth + "月" + day + "日是当年的" + sum + "天");
}
else
{
switch(mouth)
{
case 3:
sum =59+day;
break;
case 4:
sum = 90 + day;
break;
case 5:
sum = 120 + day;
break;
case 6:
sum = 151 + day;
break;
case 7:
sum = 181 + day;
break;
case 8:
sum = 212 + day;
break;
case 9:
sum = 243 + day;
break;
case 10:
sum = 273 + day;
break;
case 11:
sum = 303 + day;
break;
case 12:
sum = 333 + day;
break;
}
Console.WriteLine(year + "年" + mouth + "月" + day + "日是当年的" + sum + "天");
}
}else
{
if (mouth <= 2)
{
sum = (mouth - 1) * 31 + day;
}
else
{
switch (mouth)
{
case 3:
sum =59+day+1;
break;
case 4:
sum = 90 + day + 1;
break;
case 5:
sum = 120 + day + 1;
break;
case 6:
sum = 151 + day + 1;
break;
case 7:
sum = 181 + day + 1;
break;
case 8:
sum = 212 + day + 1;
break;
case 9:
sum = 243 + day + 1;
break;
case 10:
sum = 273 + day + 1;
break;
case 11:
sum = 303 + day + 1;
break;
case 12:
sum = 333 + day + 1;
break;
}
Console.WriteLine(year + "年" + mouth + "月" + day + "日是当年的" + sum + "天");
}
}
}
} while ((mouth < 1 || mouth > 12) || (day < 1 || day > 31));
}
}
}
是这样么 ?刚好我的作业上有这一题
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Maker
{
class Program
{
static void Main(string[] args)
{
int a = 31;
int b = 30;
int c, mouth=0, day=0,year;
int sum=0;
do
{
Console.WriteLine("请输入年:");
year=int.Parse(Console.ReadLine());
Console.WriteLine("请输入月份:");
mouth = int.Parse(Console.ReadLine());
Console.WriteLine("请输入日数:");
day = int.Parse(Console.ReadLine());
if ((mouth < 1 || mouth > 12) || (day < 1 || day > 31)||year<=0)
{
Console.WriteLine("输入有误!");
}else
{
if(year%4!=0)
{
c = 28;
if (mouth <= 2)
{
sum = (mouth - 1) * 31 + day;
Console.WriteLine(year + "年" + mouth + "月" + day + "日是当年的" + sum + "天");
}
else
{
switch(mouth)
{
case 3:
sum =59+day;
break;
case 4:
sum = 90 + day;
break;
case 5:
sum = 120 + day;
break;
case 6:
sum = 151 + day;
break;
case 7:
sum = 181 + day;
break;
case 8:
sum = 212 + day;
break;
case 9:
sum = 243 + day;
break;
case 10:
sum = 273 + day;
break;
case 11:
sum = 303 + day;
break;
case 12:
sum = 333 + day;
break;
}
Console.WriteLine(year + "年" + mouth + "月" + day + "日是当年的" + sum + "天");
}
}else
{
if (mouth <= 2)
{
sum = (mouth - 1) * 31 + day;
}
else
{
switch (mouth)
{
case 3:
sum =59+day+1;
break;
case 4:
sum = 90 + day + 1;
break;
case 5:
sum = 120 + day + 1;
break;
case 6:
sum = 151 + day + 1;
break;
case 7:
sum = 181 + day + 1;
break;
case 8:
sum = 212 + day + 1;
break;
case 9:
sum = 243 + day + 1;
break;
case 10:
sum = 273 + day + 1;
break;
case 11:
sum = 303 + day + 1;
break;
case 12:
sum = 333 + day + 1;
break;
}
Console.WriteLine(year + "年" + mouth + "月" + day + "日是当年的" + sum + "天");
}
}
}
} while ((mouth < 1 || mouth > 12) || (day < 1 || day > 31));
}
}
}
是这样么 ?刚好我的作业上有这一题
全部回答
- 1楼网友:第四晚心情
- 2021-03-16 19:03
提示里都写的很清楚了,①、将月份分为三类:31天的一类;30天的一类;然后就是2月份的。②、如果,输入的月份是2月份,就先判断下当年是否为闰年。
最后,从用户输入的年份、月份来按照①、②来计算,得到值·
- 2楼网友:过活
- 2021-03-16 18:04
1. static void Main(string [] args){ Console.WriteLine("请输入年份:"); int year = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入月份:"); int month = Convert.ToInt32(Console.ReadLine()); switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: Console.WriteLine("{0}年{1}月有31天", year, month); break; case 4: case 6: case 9: case 11: Console.WriteLine("{0}年{1}月有30天", year, month); break; case 2: Console.WriteLine("{0}年{1}月有{2}天", year, month, (28 + (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)).GetHashCode())); break; default: Console.WriteLine("输入的月份不合法"); break; }}
- 3楼网友:杯酒困英雄
- 2021-03-16 16:42
代码如下:
using system;
using system.collections.generic;
using system.text;
namespace consoleapplication1
{
class program
{
static void main(string[] args)
{
system.console.write("请输入年:");
int year = int.parse(system.console.readline());
system.console.write("请输入月:");
int month = int.parse(system.console.readline());
int []mon=new int [12]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && month ==2) //闰年
{
mon[month - 1] += 1;
system.console.writeline("{0}年的{1}月有{2}天!", year, month, mon[month-1]);
}
else //平年
{
system.console.writeline("{0}年的{1}月有{2}天!", year, month, mon[month-1]);
}
system.console.read();
}
}
}
- 4楼网友:从此江山别
- 2021-03-16 16:08
你好!是要求什么界面?年月要怎么输入?C#什么版本
- 5楼网友:执傲
- 2021-03-16 14:50
按位与&例子:inta=1,b=0;a=a&b;结果a=0=====================================表达式之间的逻辑与&&例子:if(a>b&&a>c)如果a>b并且a>c则
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯