<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 ...
随机推荐
- go 连接MSSQLServer数据库【遇到的坑】
前言:项目测试需要用到mssqlserver数据库连接,遇到坑,自己爬直接上代码: package main import ( "database/sql" " ...
- harmony OS 开发工具安装
harmony OS 开发工具安装 安装流程 安装完成 初始配置 双击打开 Running DevEco Studio requires the npm configuration informati ...
- 初学Python-day1 运算符和数据类型
- javascript-vue介绍
vue.js是一个用于创建web交互页面的库 从技术角度讲,vue专注于MVVM模型的viewModel层,它通过双向数据绑定把view层和model层连接起来,实际DOM封装和输出格式都被抽象为Di ...
- JavaScript中的模式匹配
JavaScript中的模式匹配 模式是用于转换输入数据的规则. 以将数据与一个或多个逻辑结构进行比较,将数据分解为各个构成部分,或以各种方式从数据中提取信息. 安装 JavaScript已经实现模式 ...
- kivy浮点布局
from kivy.app import App from kivy.uix.floatlayout import FloatLayout class FloatLayoutWidget(FloatL ...
- 普通用户在命令终端使用Python脚本连入校园网
普通用户在命令终端使用Python脚本连入校园网 想要连入校园网的步骤: 浏览器输入对应的IP地址,输入账号密码连网: 下载对应软件,输入账号密码连网: 而面对没有界面的服务器,而你又没有root权限 ...
- 攻防世界 杂项 12.Training-Stegano-1
题目描述: 这是我能想到的最基础的图片隐写术.啊这 题目分析: 最初还以为直接右击属性查看呢 然后用notepad++看看,一团乱码,结果在最后发现了passwd, 然后这就是flag:stegano ...
- 旋转数组的最小数字 牛客网 剑指Offer
旋转数组的最小数字 牛客网 剑指Offer 题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...
- centos 下安装docker
官方文档比较累赘,简化就三步 1.安装依赖 yum -y install gcc gcc-c++ yum-utils device-mapper-persistent-data lvm2 2.添加re ...