C#基础(3)——异常捕获与断点调试

try-catch

数字加倍:

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

namespace ChuangzhiConsel
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 0;//作用域问题
            bool is_flag = true;

            Console.WriteLine("请输入数字:");
            try
            {
                number = Convert.ToInt32(Console.ReadLine());
            }
            catch
            {
                Console.WriteLine("输入的数字不能转化!");
                is_flag = false;
            }

            if (is_flag)
            {

                Console.WriteLine("加倍后:{0}", number*2);
            }
            Console.ReadKey();

        }

    }

}

switch-case:

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

namespace ChuangzhiConsel
{
    class Program
    {
        static void Main(string[] args)
        {
            bool is_flag = true;
            Console.WriteLine("input:");
            string level = Console.ReadLine();
            double salary = 500;
            switch (level)
            {
                case "a": salary += 300;
                    break;
                case "b": salary += 200;
                    break;
                case "c": salary += 100;
                    break;
                default: Console.WriteLine("输入错误!");
                    is_flag = false;
                    break;
            }

            if (is_flag)
            {

                Console.WriteLine("翻倍工资:{0}", salary);
            }
            Console.ReadKey();


        }

    }

}

使用switch-case的定值判定分数等级:

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

namespace ChuangzhiConsel
{
    class Program
    {
        static void Main(string[] args)
        {
            bool is_flag = true;
            int level = 0;
            Console.WriteLine("Input Score:");
            try
            {

                level = Convert.ToInt32(Console.ReadLine())/10;
            }
            catch
            {
                is_flag = false;
                Console.WriteLine("输入错误!");
            }
            switch (level)
            {
                case 10: 
                case 9: Console.WriteLine("Rank:A");
                    break;
                case 8: Console.WriteLine("Rank:B");
                    break;
                case 7: Console.WriteLine("Rank:C");
                    break;
                default: Console.WriteLine("Rank:D");
                    break;
            }

            if (!is_flag)
            {

                Console.WriteLine("Rank:None");
            }
            Console.ReadKey();
        }

    }

}

判断月份天数

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

namespace ChuangzhiConsel
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("请输入年份:");
                int year = Convert.ToInt32(Console.ReadLine());
                try
                {
                    Console.WriteLine("请输入月份:");
                    int month = Convert.ToInt32(Console.ReadLine());
                    int day = 0;
                    if (month >= 1 && month <= 12)
                    {
                        switch (month)
                        {
                            case 4:
                            case 6:
                            case 9:
                            case 11: day = 30;
                                break;
                            case 2:
                                if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
                                {
                                    day = 29;
                                }
                                else
                                {
                                    day = 28;
                                }
                                break;

                            default: day = 31;
                                break;
                        }
                        Console.WriteLine("{0}年{1}月,有{2}天。", year, month, day);
                    }//判断月份
                    else
                    {
                        Console.WriteLine("月份越界!");
                    }
                }//try month 
                catch
                {
                    Console.WriteLine("输入的月份有误,程序退出!");
                }
            }//try year
            catch
            {
                Console.WriteLine("输入的年份有误,程序退出!");
            }
            Console.ReadKey();
        }
    }
}

断点调试

  • 单步运行调试F11,黄色的是即将执行的代码,
  • 逐过程调试 F10,
  • 设置观察变量,栏中写入变量或者表达式,
  • 设置断点
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何以问天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值