From 40f7ce40167a4cbf196a588355fd53c28acfd5db Mon Sep 17 00:00:00 2001 From: yinchenxu <474746235@qq.com> Date: Wed, 2 Jun 2021 21:49:07 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E6=AE=B7=E6=99=A8?= =?UTF-8?q?=E6=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\346\256\267\346\231\250\346\227\255/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\347\254\2546\346\254\241\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/.keep" 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 0000000..e69de29 -- Gitee From 0eac9277cb529fe478df20c3079ba8d9bf8e1d7c Mon Sep 17 00:00:00 2001 From: yinchenxu <474746235@qq.com> Date: Wed, 2 Jun 2021 21:51:31 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=AE=B7=E6=99=A8=E6=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Program1.cs" | 81 ++++++++++++++++ .../Program2.cs" | 94 +++++++++++++++++++ 2 files changed, 175 insertions(+) create mode 100644 "\347\254\2546\346\254\241\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/Program1.cs" create mode 100644 "\347\254\2546\346\254\241\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/Program2.cs" 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 0000000..b4a92ff --- /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 0000000..eb313fa --- /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); + } + } + } +} -- Gitee