Foreach语句,Throw语句,Try,catch语句的使用,报告编译错误

1、Foreach语句

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

namespace Foreach语句的使用
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Foreach语句的使用";
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.Clear();

            Hashtable cityHash = new Hashtable();
            cityHash.Add("010", "北京");
            cityHash.Add("020", "上海");
            cityHash.Add("030", "天津");

            Console.WriteLine("电话区号\t城市");
            foreach (string a in cityHash.Keys)
            {
                Console.WriteLine("{0}\t\t{1}", a, cityHash[a]);
            }
            Console.WriteLine();
            Console.Read();
        }
    }
}

2、Throw语句

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

namespace Throw语句的使用
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Throw语句的使用";
            Console.BackgroundColor = ConsoleColor.White;
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.Clear();

            string username = "";
            try
            {
                Console.Write("请输入用户名:");
                username = Console.ReadLine();
                if (username == "")
                    throw new ArgumentException("", "用户名不能为空!\n");
            }
            catch(ArgumentException e)
            {
                Console.ForegroundColor=ConsoleColor.Red;
                Console.WriteLine(e.Message);
            }
            finally
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                if (username != "")
                {
                    Console.WriteLine("{0}用户,您好!\n", username);
                }
            }
        }
    }
}

3、try,catch语句

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

namespace Try_Catch语句的使用
{
    class Program
    {
        static void Main(string[] args)
        {
            int x, y, z = 0;
            Console.Title = "异常处理示例";
            try
            {
                Console.Write("请输入一个整数(被除数):");
                x = int.Parse(Console.ReadLine());
                Console.Write("请输入另一个整数(除数):");
                y = int.Parse(Console.ReadLine());
                z = x / y;
            }
            catch(FormatException e)
            {
                Console.WriteLine("发生异常:{0}", e.GetType());
                Console.WriteLine(e.Message);
                Console.WriteLine("应输入一个整数");
            }
            catch (DivideByZeroException e)
            {
                Console.WriteLine("发生异常:{0}",e.GetType());
                Console.WriteLine(e.Message);
                Console.WriteLine("除数不能为0");
            }
            catch(OverflowException e)
            {
                Console.WriteLine("发生异常:{0}",e.GetType());
                Console.WriteLine(e.Message);
                Console.WriteLine("输入的数字超出整数范围");
            }
            finally
            {
                Console.WriteLine("商={0}",z);
                Console.WriteLine("谢谢使用!\n");
            }
        }
    }
}

4、#deine ERROR\#define WARNING

#define ERROR
#define WARNING
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace 报告编译错误
{
    class Program
    {
        static void Main(string[] args)
        {
#if(ERROR&&WARNING)
//#error
#warning
#endif
            Console.WriteLine("Hello,world!!\n");
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值