上午孙老师讲了如何判断闰年;
//判断是不是闰年,普通年份,是4的倍数但是不能是100的倍数;
//世纪年份是400的倍数
//输入一个年份,判断是不是闰年
//Console.Write("请输入一个年份:");
int year = int.Parse(Console.ReadLine());
if(year>0&&year<=9999)
{
if(year%4==0&&year%100!=0||year%400==0)
{
Console.WriteLine("你输入的年份数闰年!");
}
else
{
Console.WriteLine("您输入的年是平年!");
}
}
Console.ReadLine();
switch case
switch case必须与break一同使用,break是跳转语句,与switch case连用的时候是跳出最近的{};
switch(数据类型的值 变量名)
案例:
//分别输入月份、几号、输出是今年的第几天?用switch case
//每年的1,3,5,7,8,10,12月都是31天;
//2016年的2月是29天;
//其他的4,6,9,11是30天;
代码:
int m1 = 31, m2 = 29, m3 = 31, m4 = 30, m5 = 31, m6 = 30, m7 = 31, m8 = 31, m9 = 30, m10 = 31, m11 = 30, m12 = 31;
Console.Write("请输入月份:");
int m = int.Parse(Console.ReadLine());
Console.Write("请输入几号:");
int d = int.Parse(Console.ReadLine());
if (m > 0 && m <= 12)
{
if (d > 0 && d <= 31)
{
Console.ReadLine();
}
else
{
Console.WriteLine("您输入有误!!");
}
}
else
{
Console.WriteLine("您的输入有误!");
}
switch (m)
{
case 1:
Console.WriteLine("第" + d + "天");
break;
case 2:
Console.WriteLine("第" + (m1 + d) + "天");
break;
case 3:
Console.WriteLine("第" + (m1 + m2 + d) + "天");
break;
case 4:
Console.WriteLine("第" + (m1 + m2 + m3 + d) + "天");
break;
case 5:
Console.WriteLine("第" + (m1 + m2 + m3 + m4 + d) + "天");
break;
case 6:
Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + d) + "天");
break;
case 7:
Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + d) + "天");
break;
case 8:
Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + d) + "天");
break;
case 9:
Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + d) + "天");
break;
case 10:
Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + d) + "天");
break;
case 11:
Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + d) + "天");
break;
case 12:
Console.WriteLine("第" + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + d) + "天");
break;
default:
Console.WriteLine("您的输入有误!");
break;
}
Console.ReadLine();
插入图片
利用刚讲到的闰年做了一个案例:
//输入年月日,看看格式是否正确
代码:
//输入年月日,看看格式是否正确
//每年的1,3,5,7,8,10,12月都是31天;
//2016年的2月是29天;
//其他的4,6,9,11是30天;
Console.Write("请输入年份:");
int year = int.Parse(Console.ReadLine());
if (year > 0 && year <= 9999)
{
Console.Write("请输入月份:");
int m = int.Parse(Console.ReadLine());
if (m > 0 && m <= 12)
{
Console.Write("请输入几号:");
int d = int.Parse(Console.ReadLine());
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
{
if (d > 0 && d <= 31)
{
Console.WriteLine(year + "年" + m + "月" + d + "日");
}
else
{
Console.WriteLine("您输入的几号有误!");
}
}
else if (m == 4 || m == 6 || m == 9 || m == 11)
{
if (d > 0 && d <= 30)
{
Console.WriteLine(year + "年" + m + "月" + d + "日");
}
else
{
Console.WriteLine("您输入的几号有误!");
}
}
else//m==2
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
if (d > 0 && d <= 29)
{
Console.WriteLine(year + "年" + m + "月" + d + "日");
}
else
{
Console.WriteLine("您输入的几号有误!");
}
}
else
{
if (d > 0 && d <= 28)
{
Console.WriteLine(year + "年" + m + "月" + d + "日");
}
else
{
Console.WriteLine("您输入的几号有误!");
}
}
}
}
else
{
Console.WriteLine("您输入的月份有误!");
}
}
else
{
Console.WriteLine("您输入的年份有误!");
}
插入截图
今儿感觉有点入门了的意思,写起代码来比昨儿顺手了,昨儿是能听懂但是自己做题还不太顺,不知道该怎么写,可是偏偏老师讲得时候很明白,自己写就不行,练了一天今儿觉得顺手多了,好的现象不是?上面的案例同桌的陈先生用了switch case来做的,做了第几天那个题领略了switch case之后,不敢恭维还是if else顺手,觉得陈先生的脑筋很灵活,早上有个题我们用if他用了条件运算既简单又省时……有这样的小伙伴还是极好的