<C#任务导引教程>练习九
//75,异常情况
using System;
class Program
{
public static void Main()
{
Console.Write("请输入编号:");
string number = Console.ReadLine();
Console.Write("请输入工资:");
double earnings = Convert.ToDouble(Console.ReadLine());
Console.Write("请输入年龄:");
int age = Convert.ToInt32(Console.ReadLine());
int f = test(number);
int g = test(earnings);
int h = test(age);
if (f == 1 && g == 1 && h == 1)
Console.WriteLine("\n编号:{0}\n工资:{1:C2}\n年龄:{2}", number, earnings, age);
}
public static int test(string p)
{
int f = 1;
try
{
if (p[0] >= '0' && p[0] <= '9')
throw new Exception();
}
catch (Exception)
{
f = 0;
Console.WriteLine("编号错误:{0}", p[0]);
}
return f;
}
public static int test(double d)
{
int g = 1;
try
{
if (d < 0 || d > 20000)
throw new Exception();
}
catch (Exception)
{
g = 0;
Console.WriteLine("工资错误:{0}", d);
}
return g;
}
public static int test(int a)
{
int w = 1;
try
{
if (a < 18 || a > 60)
throw new Exception( );
}
catch (Exception)
{
w = 0;
Console.WriteLine("年龄错误:{0}", a);
}
return w;
}
}
//76,多重异常类型捕捉示例
using System;
class Program
{
public static void Main()
{
short sh = 0;
string s = null;
for (int i = 1; i <= 4; i++)
{
try
{
Console.Write(" 请输入一个整数 :");
s=Console.ReadLine();
sh=short.Parse(s);
double d=100/sh;
}
catch(FormatException e)
{
Console.WriteLine("格式错误!{0}",s);
}
catch (OverflowException e)
{
Console.WriteLine("数据溢出!{0}", s);
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine("主函数结束:");
}
}
}
//77,编写程序,Stud,用于输入学生的数据,当成绩score大于100或小于0时,抛出一个异常,显示出错信息后,继续输入学生的成绩
using System;
class Program
{
class Stud
{
protected int no;
protected string name;
protected int score;
public void getdata()
{
Console.Write(" 学号:");
no = Convert.ToInt32(Console.ReadLine());
Console.Write(" 姓名:");
name = Console.ReadLine();
Console.Write(" 成绩:");
score = Convert.ToInt32(Console.ReadLine());
if (score > 100 || score < 0)
throw new Exception();
}
public void getscore()
{
score = Convert.ToInt32(Console.ReadLine());
}
public void disp()
{
Console.WriteLine("学号:{0,4}姓名:{1,8}成绩:{2,3}", no, name, score);
}
}
public static void Main()
{
Stud[ ] st = new Stud[3];
Console.WriteLine("输入数据:");
for (int i = 0; i < 3; i++)
{
Console.WriteLine("请输入第{0}位学生的学号、姓名、成绩:",i+1);
st[i]=new Stud( );
try
{
st[i].getdata();
}
catch(Exception)
{
Console.Write(" 成绩不正确,重新输入:");
st[i].getscore();
}
}
Console.WriteLine("\n输出数据:");
for(int j=0;j<3;j++)
st[j].disp();
}
}
//78,公有派生方式之下对继承来的成员访问属性的变化示例
using System;
class A
{
private int x;
protected int y;
public int z;
public void setx(int i)
{
x = i;
}
public int getx()
{
return x;
}
}
class B : A
{
private int m;
protected int n;
public int p;
public void setvalue(int a, int b, int c, int d, int e, int f)
{
setx(a);
y = b;
z = c;
m = d;
n = e;
p = f;
}
public void display()
{
Console.WriteLine("x={0}", getx());
//Console.WriteLine("x={0}", x);
Console.WriteLine("y={0}", y);
Console.WriteLine("m={0}", m);
Console.WriteLine("n={0}", n);
}
}
class MainClass
{
static void Main()
{
B obj = new B();
obj.setvalue(1, 2, 3, 4, 5, 6);
obj.display();
Console.WriteLine("z={0}", obj.z);
//Console.WriteLine("m={0}", obj.m);
//Console.WriteLine("n={0}", obj.n);
Console.WriteLine("p={0}", obj.p);
}
}
//79,类的派生层次
using System;
class Person
{
protected static int no;
protected static string name;
public static void getdata()
{
Console.Write(" 编号:");
no = Convert.ToInt32(Console.ReadLine());
Console.Write(" 姓名:");
name = Console.ReadLine();
}
public static void dispdata()
{
Console.WriteLine(" 编号:{0}", no);
Console.WriteLine(" 姓名:{0}", name);
}
}
class Teacher : Person
{
protected string title;
protected string section;
public new void getdata()
{
Console.WriteLine("\n输入一个教师数据:");
Person.getdata();
Console.Write(" 职称:");
title = Console.ReadLine();
Console.Write(" 教研室:");
section = Console.ReadLine();
}
public new void dispdata()
{
Console.WriteLine("\n\n输出一个教师数据:");
Person.dispdata();
Console.WriteLine(" 职称:{0}", title);
title = Console.ReadLine();
Console.WriteLine(" 教研室:{0}", section);
section = Console.ReadLine();
}
}
class Student : Person
{
protected static string sex;
protected static string cname;
public static new void getdata()
{
Person.getdata();
Console.Write(" 性别:");
sex = Console.ReadLine();
Console.Write(" 班号:");
cname = Console.ReadLine();
}
public static new void dispdata()
{
Person.dispdata();
Console.WriteLine(" 性别:{0}", sex);
Console.WriteLine(" 班号:{0}", cname);
}
}
class Unstudent : Student
{
private int score1;
private int score2;
private int score3;
public new void getdata()
{
Console.WriteLine("\n\n输入一个大学生数据:");
Student.getdata();
Console.Write(" 大学英语:");
score1 = Convert.ToInt32(Console.ReadLine());
Console.Write(" 高等数学:");
score2 = Convert.ToInt32(Console.ReadLine());
Console.Write(" 程序设计:");
score3 = Convert.ToInt32(Console.ReadLine());
}
public new void dispdata()
{
Console.WriteLine("\n\n输出一个大学生数据:");
Student.dispdata();
Console.WriteLine(" 大学英语:{0}", score1);
Console.WriteLine(" 高等数学:{0}", score2);
Console.WriteLine(" 程序设计:{0}", score3);
Console.WriteLine(" 平均分:{0:F1}", (score1+score2+score3)/3.0f);
}
}
class Mistudent : Student
{
private int score1;
private int score2;
private int score3;
public new void getdata()
{
Console.WriteLine("\n\n输入一个中学生数据:");
Student.getdata();
Console.Write(" 英语:");
score1 = Convert.ToInt32(Console.ReadLine());
Console.Write(" 数学:");
score2 = Convert.ToInt32(Console.ReadLine());
Console.Write(" 语文:");
score3 = Convert.ToInt32(Console.ReadLine());
}
public new void dispdata()
{
Console.WriteLine("\n\n输出一个大学生数据:");
Student.dispdata();
Console.WriteLine(" 英语:{0}", score1);
Console.WriteLine(" 数学:{0}", score2);
Console.WriteLine(" 语文:{0}", score3);
Console.WriteLine(" 平均分:{0:F1}", (score1 + score2 + score3) / 3.0f);
}
}
class MainClass
{
static void Main()
{
Teacher t = new Teacher();
t.getdata();
t.dispdata();
Unstudent s1 = new Unstudent();
s1.getdata();
s1.dispdata();
Mistudent s2 = new Mistudent();
s1.getdata();
s2.dispdata();
}
}
//80,声明一个接口ISharp,用于计算矩形面积
using System;
public interface ISharp
{
double Width { get; set; }
double GetArea();
}
public class Rectangle : ISharp
{
public double dwidth;
public double dheight;
public Rectangle(double w, double h)
{
this.dwidth = w;
this.dheight = h;
}
public double Width
{
get { return dwidth; }
set { dwidth = value; }
}
public double Height
{
get { return dheight; }
set { dheight = value; }
}
public double GetArea()
{
return this.dheight * this.dwidth;
}
}
class Program
{
static void Main()
{
Rectangle rt = new Rectangle(4, 5);
Console.WriteLine("矩形面积为:{0:f}", rt.GetArea());
rt.Width = 8;
Console.WriteLine("矩形面积为:{0:f}", rt.GetArea());
}
}
/*81,创建一个控制台程序,其中声明了两个接口,ImyInterface1和ImyInterface2,在这两个接口中声明了一个同名方法Add,然后定义一个类MyClass,
该类继承自己已经声明的两个接口。在MyClass类中实现接口中的方法时,由于ImyInterface1和ImyInterface2接口中声明的方法名相同,这里使用了显式接口成员实现,
最后在主程序类Program的Main方法中使用接口对象调用接口中定义的方法*/
using System;
interface ImyInterface1
{
int Add();
}
interface ImyInterface2
{
int Add();
}
class myClass : ImyInterface1, ImyInterface2
{
int ImyInterface1.Add()
{
int x = 98;
int y = 368;
return x + y;
}
int ImyInterface2.Add()
{
int x = 98;
int y = 368;
int z = 698;
return x + y + z;
}
}
class Program
{
static void Main()
{
myClass myclass = new myClass();
ImyInterface1 imyinterface1 = myclass;
Console.WriteLine("x+y={0}", imyinterface1.Add());
ImyInterface2 imyinterface2 = myclass;
Console.WriteLine("x+y+z={0}", imyinterface2.Add());
}
}
/*82,使用接口完成多继承问题*/
using System;
interface ITeacher
{
string Name
{
get;
set;
}
int Age
{
get;
set;
}
string Title
{
get;
set;
}
}
interface IStudent
{
string Sex
{
get;
set;
}
double Score
{
get;
set;
}
}
class Graduate : ITeacher, IStudent
{
string name = " ";
public string Name
{
get { return name; }
set { name = value; }
}
int age = 0;
public int Age
{
get { return age; }
set { age = value; }
}
string title = " ";
public string Title
{
get { return title; }
set { title = value; }
}
string sex = " ";
public string Sex
{
get { return sex; }
set { sex = value; }
}
double score =0.0;
public double Score
{
get { return score; }
set { score = value; }
}
public void Set(double w)
{
wage = w;
}
public void show()
{
Console.WriteLine(" 基本信息: ");
Console.WriteLine(" 姓名:{0}",name);
Console.WriteLine(" 性别:{0}",sex);
Console.WriteLine(" 年龄:{0}",age);
Console.WriteLine(" 职称:{0}",title);
Console.WriteLine(" 工资:{0}",wage);
Console.WriteLine(" 成绩:{0}",score);
}
private double wage;
}
class Program
{
static void Main()
{
Graduate grad = new Graduate();
Console.Write("请输入姓名:");
grad.Name = Console.ReadLine();
Console.Write("请输入性别:");
grad.Sex =Console.ReadLine();
Console.Write("请输入年龄:");
grad.Age = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入职称:");
grad.Sex = Console.ReadLine();
Console.Write("请输入工资:");
grad.Set(Convert.ToDouble(Console.ReadLine()));
Console.Write("请输入成绩:");
grad.Score = Convert.ToDouble(Console.ReadLine());
grad.show();
}
}
<C#任务导引教程>练习九的更多相关文章
- 无废话ExtJs 入门教程十九[API的使用]
无废话ExtJs 入门教程十九[API的使用] extjs技术交流,欢迎加群(201926085) 首先解释什么是 API 来自百度百科的官方解释:API(Application Programmin ...
- Senparc.Weixin.MP SDK 微信公众平台开发教程(九):自定义菜单接口说明
上一篇<Senparc.Weixin.MP SDK 微信公众平台开发教程(八):通用接口说明>介绍了如何通过通用接口获取AccessToken,有了AccessToken,我们就可以来操作 ...
- Docker入门教程(九)10个镜像相关的API
Docker入门教程(九)10个镜像相关的API [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第九篇,重点介绍了镜像相关的Docker Remote ...
- iOS 11开发教程(九)iOS11数据线连接真机测试
iOS 11开发教程(九)iOS11数据线连接真机测试 在Xcode 7.0之后,苹果公司在开发许可权限上做了很多的改变,在测试App方面取消了一些限制.在Xcode7.0之前的版本,苹果公司只向注册 ...
- ComicEnhancerPro 系列教程十九:用JpegQuality看JPG文件的压缩参数
作者:马健邮箱:stronghorse_mj@hotmail.com 主页:http://www.comicer.com/stronghorse/ 发布:2017.07.23 教程十九:用JpegQu ...
- struts2官方 中文教程 系列九:Debugging Struts
介绍 在Struts 2 web应用程序的开发过程中,您可能希望查看由Struts 2框架管理的信息.本教程将介绍两种工具,您可以使用它们来查看.一个工具是Struts 2的配置插件,另一个是调试拦截 ...
- 从壹开始前后端分离 [ vue + .netcore 补充教程 ] 二九║ Nuxt实战:异步实现数据双端渲染
回顾 哈喽大家好!又是元气满满的周~~~二哈哈,不知道大家中秋节过的如何,马上又是国庆节了,博主我将通过三天的时间,给大家把项目二的数据添上(这里强调下,填充数据不是最重要的,最重要的是要配合着让大家 ...
- angular2系列教程(九)Jsonp、URLSearchParams、中断选择数据流
大家好,今天我们要讲的是http模块的第二部分,主要学习ng2中Jsonp.URLSearchParams.observable中断选择数据流的用法. 例子
- Linux系列教程(九)——Linux常用命令之网络和关机重启命令
前一篇博客我们讲解了Linux压缩和解压缩命令,使用的最多的是tar命令,因为现在很多源码包都是.tar.gz的格式,通过 tar -zcvf 能完成解压.然后对于.zip格式的文件,使用gunzip ...
随机推荐
- 2019 年 CNCF 中国云原生调查报告
中国 72% 的受访者生产中使用 Kubernetes 在 CNCF,为更好地了解开源和云原生技术的使用,我们定期调查社区.这是第三次中国云原生调查,以中文进行,以便更深入地了解中国云原生技术采用的步 ...
- 关于dp那些事
拿到一道题,先写出状态转移方程,再优化时间复杂度 状态优化: 对于状态可累加 \(e.g.dp[i+j]=dp[i]+dp[j]+i+j\) 的,用倍增优化 决策优化: \(e.g.dp[i][j]= ...
- Linux系统安装MySql5.7并通过sql脚本导入数据
为了下载到的MySQL版本和目标系统相互兼容,在开启之前,最好了解目标系统的相关信息. 查询系统版本: cat /etc/issue 查看系统位数 getconf LONG_BIT 选择MySQL 根 ...
- Docker--harbor私有仓库部署与管理
目录 一.Harbor简介 二.Harbor 部署 三.维护管理Harbor 一.Harbor简介 1.什么是Harbor ? Harbor 是 VMware 公司开源的企业级 Docker Re ...
- 小数的十进制和二进数转换 “无限不循环”小数的IEEE 754表示
十进制 -> 二进制 将整数部分和小数部分分开处理 例:3.125(10) 其整数部分为11(2) 小数部分按照下面的步骤求解: 0.125 x 2 = 0.25 取0 0.250 x 2 = ...
- CORS+XSS的漏洞利用payload
之前有人问我有没有CORS+XSS的利用姿势,翻了一下国内貌似都没有利用姿势于是就写了这篇文章!!! 首先找到一个反射xss,然后使用xss加载javascript代码达到跨域劫持目的payload如 ...
- Markdown Reference
Markdown For Typora Overview Markdown is created by Daring Fireball; the original guideline is here. ...
- 23.合并k个有序链表
合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1->3->4, 2->6 ] 输出: 1-&g ...
- DevOps 时代的高效测试之路
10 月 22 日,2021 届 DevOps 国际峰会在北京顺利召开,来自国内外的顶级技术专家共同畅谈 DevOps 体系与方法.过程与实践.工具与技术.CODING 测试及研发流程管理产品总监程胜 ...
- 人人都写过的5个Bug!
大家好,我是良许. 计算机专业的小伙伴,在学校期间一定学过 C 语言.它是众多高级语言的鼻祖,深入学习这门语言会对计算机原理.操作系统.内存管理等等底层相关的知识会有更深入的了解,所以我在直播的时候, ...