C#第九节课

try catch

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace trycatch
{
class Program
{
static void Main(string[] args)
{
//异常语句try catch finally
//try//保护执行里面的代码段,若其中一句有误,直接跳转到catch
//{//不管下面的内容
// Console.Write("请输入一个整数");
// int a = int.Parse(Console.ReadLine());
// Console.WriteLine("hello");
//}
//catch//try中发现异常,直接执行,若try中无错,不执行
//{
// Console.WriteLine("输入有误!");
//}
//finally //不管对错都执行
//{
// Console.WriteLine("谢谢使用,再见");
//}
//Console.ReadLine();

 

// System.Threading.Thread.Sleep(2000);//间隔2秒

int m1 = 31, m3 = 31, m4 = 30, m5 = 31, m6 = 30, m7 = 31, m8 = 31, m9 = 30, m10 = 31, m11 = 30, m12 = 31;
int m2 = 0;
int year;
string y;
int a = 0;
int b = 0;
for (; ; )
{
for (; ; )
{
try
{

Console.WriteLine("请输入一个年份: ");
year = int.Parse(Console.ReadLine());
break;
}
catch
{
Console.WriteLine("您输入的年份格式有误");
}
}
if (year >= 0 && year <= 9999)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
y = "您输入的年份是闰年!";
m2 = 29;
}
else
{
y = "您输入的是平年!";
m2 = 28;
}
for (; ; )
{
for (; ; )
{
try
{
Console.WriteLine("请输入月份:");
a = int.Parse(Console.ReadLine());
break;
}
catch
{
Console.WriteLine("您输入的月份格式有误");
}
}

if (a > 0 && a < 13)
{

for (; ; )
{
for (; ; )
{
try
{
Console.Write("请输入日: ");
b = int.Parse((Console.ReadLine()));
break;
}
catch
{
Console.WriteLine("您输入的日期格式有误");
}
}

if ((((a == 1 || a == 3 || a == 5 || a == 7 || a == 8 || a == 10 || a == 12)) && (b > 0 && b < 32)) || ((a == 4 || a == 6 || a == 9 || a == 11) && (b > 0 && b < 31)) || ((a == 2) && (b > 0 && b < (m2 + 1))))
{
switch (a)
{
case 1:
Console.WriteLine("今年过了 " + (b) + " 天了");
break;
case 2:
Console.WriteLine("今年过了 " + (m1 + b) + " 天了");
break;
case 3:
Console.WriteLine("今年过了 " + (m1 + m2 + b) + " 天了");
break;
case 4:
Console.WriteLine("今年过了 " + (m1 + m2 + m3 + b) + " 天了");
break;
case 5:
Console.WriteLine("今年过了 " + (m1 + m2 + m3 + m4 + b) + " 天了");
break;
case 6:
Console.WriteLine("今年过了 " + (m1 + m2 + m3 + m4 + m5 + b) + " 天了");
break;
case 7:
Console.WriteLine("今年过了 " + (m1 + m2 + m3 + m4 + m5 + m6 + b) + " 天了");
break;
case 8:
Console.WriteLine("今年过了 " + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + b) + " 天了");
break;
case 9:
Console.WriteLine("今年过了 " + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + b) + " 天了");
break;
case 10:
Console.WriteLine("今年过了 " + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + b) + " 天了");
break;
case 11:
Console.WriteLine("今年过了 " + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + b) + " 天了");
break;
case 12:
Console.WriteLine("今年过了 " + (m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + b) + " 天了");
break;
default:
Console.WriteLine("您输入的有误");
break;
}
Console.WriteLine(y + "日期是" + year + "年" + a + "月" + b + "日");

break;

}
else
{
Console.WriteLine("您输入的日有误");
}
} break;


}
else
{
Console.WriteLine("您输入的月份有误");
}


}


}

else
{
Console.WriteLine("您输入的年份有误");
}

}
}

}

posted @ 2016-03-11 14:42  熊小白  阅读(114)  评论(0编辑  收藏  举报