C#学习 Day4

  Try Catch的使用方法

C#中异常捕获

try

{

 有可能出现错误的代码写在这里

}

catch

{

  出错后的处理

}

如果try中的代码没有出错,则程序正常运行try中的哪容后,不会执行catch中的内容

如果try中的代码一旦出错,程序立即跳入catch中去执行代码,那么try中的出错代码后面不再执行catch中的内容;

 代码实例

//练习1输入姓名 科目(语数外) 编程球总分和平均分
            //在屏幕上显示:XXX的总分为 XX分
            try
            {
                Console.WriteLine("请输入你的姓名");
                string name = Console.ReadLine();
                Console.WriteLine("请输入你的语文成绩");
                int chinese = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入你的数学成绩");
                int math = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入你的英语成绩");
                int english = Convert.ToInt32(Console.ReadLine());

                int sum = chinese + math + english;
                double avg = (chinese + math + english) / 3;
                Console.WriteLine("{0}的三门总分为{1}分,平均分为{2}", name, sum, avg);
            }
            catch
            {
                Console.Write("麻痹你输入错误,重新运行");
            }

            Console.ReadKey();

示例代码2

 

 ///*练习2 编程实现计算几天(如46天)是几周零几天
            //解题思路,一周 =7天;所以给days赋值 求模,能整除的部分就是几周
            //*/
            try
            {
                Console.WriteLine("请输入你要计算的天数");
                int days = Convert.ToInt32(Console.ReadLine());
                int week = days / 7;
                int mod = days % 7;
                Console.WriteLine("{0}中共有{1}周{2}天", days, week, mod);
                Console.ReadKey();
            }
            catch
            {
                Console.WriteLine("对不起,您的输入有误请重新输入");
                Console.ReadKey();

实例代码3

  //第三题 编程实现107653秒是几天几小时几分几秒?

            int seconds = 107653;
            int days = seconds / (3600 * 24);
            int mod = seconds % (3600 * 24);//除去上面的天数还剩多少秒
            int hours = mod / 3600;//剩余的秒数
            mod = mod % 3600;//剩余的描述除去上面的小时,还剩多少秒
            int min = mod / 60;//看看剩余的秒数中有几个60秒,就是还有几分钟
            int second = mod % 60;
            Console.WriteLine("{0}中包含{1}天{2}小时{3}分钟{4}秒", seconds, days, hours, min, second);
            Console.ReadKey();

 

///*练习2 编程实现计算几天(如46天)是几周零几天
            //解题思路,一周 =7天;所以给days赋值 求模,能整除的部分就是几周
            //*/
            //try
            //{
            //    Console.WriteLine("请输入你要计算的天数");
            //    int days = Convert.ToInt32(Console.ReadLine());
            //    int week = days / 7;
            //    int mod = days % 7;
            //    Console.WriteLine("{0}中共有{1}周{2}天", days, week, mod);
            //    Console.ReadKey();
            //}
            //catch
            //{
            //    Console.WriteLine("对不起,您的输入有误请重新输入");
            //    Console.ReadKey();
            //}
            //第三题 编程实现107653秒是几天几小时几分几秒?

            int seconds = 107653;
            int days = seconds / (3600 * 24);
            int mod = seconds % (3600 * 24);//除去上面的天数还剩多少秒
            int hours = mod / 3600;//剩余的秒数
            mod = mod % 3600;//剩余的描述除去上面的小时,还剩多少秒
            int min = mod / 60;//看看剩余的秒数中有几个60秒,就是还有几分钟
            int second = mod % 60;
            Console.WriteLine("{0}中包含{1}天{2}小时{3}分钟{4}秒", seconds, days, hours, min, second);
            Console.ReadKey();

 

posted @ 2013-10-21 21:17  Blackyxc  阅读(118)  评论(0编辑  收藏  举报