diff --git "a/\347\254\2546\346\254\241\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/.keep" "b/\347\254\2546\346\254\241\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\2546\346\254\241\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/Program1.cs" "b/\347\254\2546\346\254\241\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/Program1.cs" new file mode 100644 index 0000000000000000000000000000000000000000..b4a92fff6f279b00a9e9242763f22825e6e2c0bb --- /dev/null +++ "b/\347\254\2546\346\254\241\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/Program1.cs" @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp1 +{ + class Program1 + { + public static Dictionary list = new Dictionary(); + static void Main(string[] args) + { + //第一题: + //一个考试管理系统,需要录入考生成绩,只能录入数字,否则会报类型转换异常。 + //请编写相关代码, + //1、捕获FormatException异常,并打印输出“异常已处理; + //2、捕获OverflowException异常,数值超出float范围的异常,并打印输出“异常已处理; + //3、捕获一般异常Exception异常。 + //4、最终处理finally + //录入成绩结束后,请输出,总学生数,总分数,平均分 + float score = 0; + Console.WriteLine("请输入学生的学号:"); + string str = Console.ReadLine(); + if (!list.ContainsKey(str)) + { + try + { + Console.WriteLine("请输入该学生的成绩:"); + score = float.Parse(Console.ReadLine()); + } + catch (FormatException e) + { + Console.WriteLine("转换异常"); + Console.WriteLine("异常已处理"); + Console.WriteLine(e.Message); + } + catch (OverflowException e) + { + Console.WriteLine("数值超出float范围"); + Console.WriteLine("异常已处理"); + Console.WriteLine(e.Message); + } + catch (Exception e) + { + Console.WriteLine("捕获一般异常"); + Console.WriteLine("异常已处理"); + Console.WriteLine(e.Message); + } + finally + { + list.Add(str, score); + Console.WriteLine("录入成功!"); + Console.WriteLine("请选择:1.继续录入 2.结束录入"); + int a = int.Parse(Console.ReadLine()); + if (a == 1) + { + Main(null); + } + else if (a == 2) + { + Console.WriteLine("总学生数:{0}",list.Count); + float sum = 0; + foreach (var item in list.Values) + { + sum += item; + } + Console.WriteLine("总成绩为:{0}",sum); + Console.WriteLine("平均分为:{0}",(sum/list.Count)); + + } + } + } + else + { + Console.WriteLine("你的输入有误或学号重复!"); + Main(null); + } + } + } +} diff --git "a/\347\254\2546\346\254\241\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/Program2.cs" "b/\347\254\2546\346\254\241\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/Program2.cs" new file mode 100644 index 0000000000000000000000000000000000000000..eb313fa3a6e314a9f63743ce91026f041f486a99 --- /dev/null +++ "b/\347\254\2546\346\254\241\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/Program2.cs" @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp2 +{ + class Program2 + { + public static float money = 0; + static void Main(string[] args) + { + //第二题: + //编写一个程序,用以接收用户输入的两个浮点型的数值,一个值表示用户想要存放在银行账户中的余额,另一个值表示用户想要从银行账户中提取的金额。 + //实现存取款功能, + //用户功能选择:1、存款,2、取款 + //取款时: + //当用户想要提取的金额大于余额时,请手动抛出一个ArgumentOutOfRangeException 参数超出范围的异常,异常信息可以写“取款金额大于余额,请重新输入” + //,如此以确保取款金额始终不大于当前余额。 + //操作结束,打印输出余额是多少。 + //最后再添加catch一般异常 Exception,使得数据类型转换的异常也能被捕获到。 + Console.WriteLine("请输入:1.存款 2.取款"); + int a = int.Parse(Console.ReadLine()); + switch (a) + { + case 1: + deposit(ref money); + break; + case 2: + withdrawal(ref money); + break; + default: + Console.WriteLine("输入错误!请重新输入!"); + Main(null); + break; + } + } + static void deposit(ref float money) + { + float a = 0 ; + try + { + Console.WriteLine("请输入存的金额:"); + a = float.Parse(Console.ReadLine()); + } + catch (Exception e) + { + Console.WriteLine("捕获异常"); + Console.WriteLine("处理成功"); + Console.WriteLine(e.Message); + } + finally + { + money += a ; + Console.WriteLine("余额:{0}",money); + Main(null); + } + } + static void withdrawal(ref float money) + { + try + { + Console.WriteLine("请输入取的金额:"); + float a = float.Parse(Console.ReadLine()); + if (a > money) + { + throw new ArgumentOutOfRangeException(); + } + else + { + money -= a ; + Console.WriteLine("余额:{0}",money); + } + } + catch (ArgumentOutOfRangeException e) + { + Console.WriteLine("取款金额大于余额,请重新输入"); + Console.WriteLine("异常已处理"); + Console.WriteLine(e.Message); + } + catch (Exception e) + { + Console.WriteLine("捕获异常"); + Console.WriteLine("异常已处理"); + Console.WriteLine(e.Message); + } + finally + { + Main(null); + } + } + } +}