C#之咿呀学语(2)

来源:互联网 发布:数据交换接口规范 编辑:程序博客网 时间:2024/06/09 17:00
//goto语句跳出嵌套循环完成数字的查找int x = 200, y = 4;            int count = 0;            string[,] array = new string[x, y];            for (int i = 0; i < x; i++)                for (int j = 0; j < y; j++)                    array[i, j] = (++count).ToString();            Console.Write("输出结果为:\n输入查找的数字:");            string myNumber = Console.ReadLine();            for (int i = 0; i < x; i++)            {                for (int j = 0; j < y; j++)                {                    if (array[i, j].Equals(myNumber))                    {                        goto Found;                    }                }            }            Console.WriteLine("数字{0}无法找到。",myNumber );            goto Finish;        Found:            Console.WriteLine("数字{0}存在",myNumber );    Finish:        Console.WriteLine("结束查找");        Console.Read();
//通过return语句把CalculateArea()方法的结果值返回给变量areastatic double CalculateArea(int r)        {            double area = r * r * Math.PI;            return area;        }        static void Main()        {            int radius = 4;            Console.WriteLine("输出结果为:\nThe area is {0:0.00}",CalculateArea (radius));            Console.Read();        }


 

//通过Exception派生了一个新异常类UserEmployeeException,该类定义了3个构造函数,每个构造函数使用不同的参数,然后再抛出自定义异常。//异常处理语句        public class UserEmployeeException : Exception        {            private string errorinfo = string.Empty;            public UserEmployeeException(string message)                : base(message)            {                errorinfo = message;            }            public UserEmployeeException(string message, Exception inner)                : base(message, inner)            {                errorinfo = message;                inner = null;            }        }        public static void Main()        {            try            {                throw new UserEmployeeException("error Info of use");            }            catch (UserEmployeeException ey)            {                Console.WriteLine("输出结果为:");                Console.WriteLine(ey.Message,ey.InnerException );                Console.Read();            }        }


 

//try-catch语句写入多个catch的使用,通过两个catch语句进行捕获异常,他们分别是ArgumentNullException异常和Exception异常static void ProcessString(string str)        {            if (str==null  )            {                throw new ArgumentNullException();            }        }        static void Main()        {            Console.WriteLine("输出结果是:");            try            {                string str = null;                ProcessString(str);            }            catch (ArgumentNullException e)            {                Console.WriteLine("{0} First exception.", e.Message);            }            catch (Exception e)            {                Console.WriteLine("{0} Second exception",e.Message );            }            Console.Read();        }


 

//try-catch-finally语句使用//有一个导致异常的无效转换语句,当运行程序时用户会收到一条运行出错的信息,但finally子句仍继续执行并显示输出。 static void Main()        {            int i = 123;            string s = "Some string";            object o = s;            try            {                i = (int)o;            }            catch { }            finally            {                Console.WriteLine("输出的结果为:");                Console.Write("i={0}",i);            }            Console.Read();