//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#任务导引教程>练习九的更多相关文章

  1. 无废话ExtJs 入门教程十九[API的使用]

    无废话ExtJs 入门教程十九[API的使用] extjs技术交流,欢迎加群(201926085) 首先解释什么是 API 来自百度百科的官方解释:API(Application Programmin ...

  2. Senparc.Weixin.MP SDK 微信公众平台开发教程(九):自定义菜单接口说明

    上一篇<Senparc.Weixin.MP SDK 微信公众平台开发教程(八):通用接口说明>介绍了如何通过通用接口获取AccessToken,有了AccessToken,我们就可以来操作 ...

  3. Docker入门教程(九)10个镜像相关的API

    Docker入门教程(九)10个镜像相关的API [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第九篇,重点介绍了镜像相关的Docker Remote ...

  4. iOS 11开发教程(九)iOS11数据线连接真机测试

    iOS 11开发教程(九)iOS11数据线连接真机测试 在Xcode 7.0之后,苹果公司在开发许可权限上做了很多的改变,在测试App方面取消了一些限制.在Xcode7.0之前的版本,苹果公司只向注册 ...

  5. ComicEnhancerPro 系列教程十九:用JpegQuality看JPG文件的压缩参数

    作者:马健邮箱:stronghorse_mj@hotmail.com 主页:http://www.comicer.com/stronghorse/ 发布:2017.07.23 教程十九:用JpegQu ...

  6. struts2官方 中文教程 系列九:Debugging Struts

    介绍 在Struts 2 web应用程序的开发过程中,您可能希望查看由Struts 2框架管理的信息.本教程将介绍两种工具,您可以使用它们来查看.一个工具是Struts 2的配置插件,另一个是调试拦截 ...

  7. 从壹开始前后端分离 [ vue + .netcore 补充教程 ] 二九║ Nuxt实战:异步实现数据双端渲染

    回顾 哈喽大家好!又是元气满满的周~~~二哈哈,不知道大家中秋节过的如何,马上又是国庆节了,博主我将通过三天的时间,给大家把项目二的数据添上(这里强调下,填充数据不是最重要的,最重要的是要配合着让大家 ...

  8. angular2系列教程(九)Jsonp、URLSearchParams、中断选择数据流

    大家好,今天我们要讲的是http模块的第二部分,主要学习ng2中Jsonp.URLSearchParams.observable中断选择数据流的用法. 例子

  9. Linux系列教程(九)——Linux常用命令之网络和关机重启命令

    前一篇博客我们讲解了Linux压缩和解压缩命令,使用的最多的是tar命令,因为现在很多源码包都是.tar.gz的格式,通过 tar -zcvf 能完成解压.然后对于.zip格式的文件,使用gunzip ...

随机推荐

  1. golang引用第三方包的报错:no required module provides package [完美解决]

    关于golang第三方包的引用报错:no required module provides package : go.mod file not found in current directory o ...

  2. 题解 「2017 山东一轮集训 Day7」逆序对

    题目传送门 Description 给定 $ n, k $,请求出长度为 $ n $ 的逆序对数恰好为 $ k $ 的排列的个数.答案对 $ 10 ^ 9 + 7 $ 取模. 对于一个长度为 $ n ...

  3. 2020.12.3--vj个人赛补题

    A Vasya studies music.He has learned lots of interesting stuff. For example, he knows that there are ...

  4. struts2漏洞复现分析合集

    struts2漏洞复现合集 环境准备 tomcat安装 漏洞代码取自vulhub,使用idea进行远程调试 struts2远程调试 catalina.bat jpda start 开启debug模式, ...

  5. NX二次开发-调内部函数UGS::UICOMP_enum::set_width(int)更改BlockUI的枚举控件宽度

    版本 NX11+VS2013 内容说明 这个内部函数的设置方法,我之前不会,是QQ群里的一位大佬分享出来的. 关于这块,我也百度搜了一下,找到了几个相关的. 1.直接手动修改BlockUI界面 在低版 ...

  6. SharkCTF2021 bybypass&baby_phpserialize题记

    (国庆褪10天了 先水一篇) bybypass: payload:?anime_is_bae=hehellotherehoomanllotherehooman baby_phpserialize ro ...

  7. 【UE4 C++】UObject 创建、销毁、内存管理

    UObject 的创建 NewObject 模板类 本例使用 UE 4.26,只剩下 NewObject 用来创建 UObject,提供两个带不同可选参数构造函数的模板类 Outer 表示这个对象的外 ...

  8. [软工顶级理解组] 团队任务拆解(Alpha)

    一.任务概述 在alpha阶段,我们需要完成功能规格说明书中所提到的所有功能,在一个阶段的开发周期内,交付最小可行的可用版本. 二.任务分配及时长 分组&成员 具体任务 预计时长(小时) 前端 ...

  9. [技术博客]大闸蟹的技术博客,通过gitlab api进行用户批量创建

    技术博客--通过gitlab api批量注册用户 gitlab登录界面本身提供了register功能,但需要手工一个个添加,对于一次性会添加整个班级的学生的软工平台来说并不科学合理.使用gitlab ...

  10. NKOJ1828 Feed Ratios饲料调配

    题目 好题!高斯消元切了! (其实只是单纯地想吐槽这道出现在"高斯消元"专练里的题,暴搜能过,goudoubuxie"Gauss") 下面是暴搜: #pragm ...