//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. js Promise用法

    Promise 英文意思是 承诺的意思,是对将来的事情做了承诺, Promise 有三种状态, Pending 进行中或者等待中 Fulfilled 已成功 Rejected 已失败 Promise ...

  2. CF1082E Increasing Frequency (multiset+乱搞+贪心)

    题目大意: \(给你n个数a_i,给定一个m,你可以选择一个区间[l,r],让他们区间加一个任意数,然后询问一次操作完之后,最多能得到多少个m\) QWQ 考场上真的** 想了好久都不会,直到考试快结 ...

  3. 基于TLS证书手动部署kubernetes集群

      一.简介 Kubernetes是Google在2014年6月开源的一个容器集群管理系统,使用Go语言开发,Kubernetes也叫K8S. K8S是Google内部一个叫Borg的容器集群管理系统 ...

  4. 创建第一个Android项目

    目录 创建第一个Android项目 创建HelloWorld项目 选择模板 选择模板界面的英文翻译 配置项目 配置项目界面英文翻译及解释 配置项目界面的注意事项 Name的命名规范 Package n ...

  5. NX Open,怎样取到面的环LOOP

    在封装的ufun .NET库里面,对UF_MODL_ask_face_loops这个函数并没有封装,导致我们很多不便,那我们在.NET下怎样才能使用这个函数呢??当然是手动处理一下 Public Fu ...

  6. 12. 亿级流量电商系统JVM模型参数二次优化

    亿级流量电商系统JVM模型参数预估方案,在原来的基础上采用ParNew+CMS垃圾收集器 一.亿级流量分析及jvm参数设置 1. 需求分析 大促在即,拥有亿级流量的电商平台开发了一个订单系统,我们应该 ...

  7. Java:并发笔记-06

    Java:并发笔记-06 说明:这是看了 bilibili 上 黑马程序员 的课程 java并发编程 后做的笔记 5. 共享模型之无锁 本章内容 CAS 与 volatile 原子整数 原子引用 原子 ...

  8. Alpha发布声明

    项目 内容 这个作业属于哪个课程 2021春季软件工程(罗杰 任健) 这个作业的要求在哪里 Alpha-发布声明 我们是谁 删库跑路对不队 我们在做什么 题士 进度如何 进度总览 一.功能与特性 1. ...

  9. Noip模拟32(再度翻车) 2021.8.7

    T1 Smooth 很水的一道题...可是最傻    的是考场上居然没有想到用优先队列优化... 上来开题看到这个,最一开始想,这题能用模拟短除法,再一想太慢了,就想着优化 偏偏想到线性筛然后试别的素 ...

  10. 热身训练1 Problem B. Harvest of Apples

    http://acm.hdu.edu.cn/showproblem.php?pid=6333 题意: 求 C(0,n)+C(1,n)+...+C(m,n) 分析: 这道题,我们令s(m,n) = C( ...