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();
- C#之咿呀学语(2)
- C#之咿呀学语(1)
- AR 恐龙博物馆四(萌宠小恐成长日记之咿呀学语)
- 咿呀
- 咿呀呀...
- C#速学之路
- 一点一点学C#2
- 【小唐软件公众号文章】第三期:人工智能之“咿呀学步”
- 风车咿呀呀
- 学C#
- 咿呀,字典是无序的
- JAVA C# C/C++之比较学:参数传递
- JAVA C# C/C++之比较学:初始化顺序
- 刚学C#心得
- C#学Java:东施效颦
- 毕业生学C#
- 今天开始学c#
- 新手学C#
- 探秘PHP 5的对象重载技术
- Android实战技巧:组件间通信---Intent和IntentFilter
- android使用pull的方式解析xml
- Linux数据安装出现的问题
- 想学习C++的童鞋们,不得不看看‘标准库头文件’有哪些
- C#之咿呀学语(2)
- Android SQLiter cursor的使用
- JSTL fmt:formatNumber日期、 数字、货币格式化
- 第一次作业小结
- javascript this 详解
- 写一个函数,求两个整数的之和,要求在函数体内不得使用+、-、×、÷
- 列举系统进程及其所调用的模块文件(Delphi XE2)
- 64
- 在Linux上开发Android,装手机驱动