C#之咿呀学语(2)

//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()方法的结果值返回给变量area static 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();



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值