using System;

class Person { protected int no; // 编号 public virtual void display() // 输出相关信息 { } }

class Student : Person { private int[] scores = new int[5]; // 5 门课程成绩

public override void display()
{
    Console.Write(no + " ");
    int cnt = 0, sum = 0;
    for (int i = 0; i < 5; i++)
    {
        if (scores[i] == -1)
        {
            cnt++;
        }
        else
        {
            sum += scores[i];
        }
    }
    Console.Write(cnt + " ");
    if (cnt == 0)
    {
        Console.WriteLine((int)Math.Round((double)sum / 5));
    }
    else
    {
        Console.WriteLine();
    }
}

public Student(int no, int[] scores)
{
    this.no = no;
    for (int i = 0; i < 5; i++)
    {
        this.scores[i] = scores[i];
    }
}

}

class Teacher : Person { private int[] papers = new int[3]; // 3 年的论文数

public override void display()
{
    Console.WriteLine(no + " " + (papers[0] + papers[1] + papers[2]));
}

public Teacher(int no, int[] papers)
{
    this.no = no;
    for (int i = 0; i < 3; i++)
    {
        this.papers[i] = papers[i];
    }
}

}

class Program { static void Main(string[] args) { Person[] persons = new Person[10]; int cnt = 0; while (true) { string[] str = Console.ReadLine().Split(); int type = int.Parse(str[0]); if (type == 0) { break; } int no = int.Parse(str[1]); if (type == 1) { int[] scores = new int[5]; for (int i = 0; i < 5; i++) { scores[i] = int.Parse(str[i + 2]); } persons[cnt++] = new Student(no, scores); } else if (type == 2) { int[] papers = new int[3]; for (int i = 0; i < 3; i++) { papers[i] = int.Parse(str[i + 2]); } persons[cnt++] = new Teacher(no, papers); } } for (int i = 0; i < cnt; i++) { persons[i].display(); } } }

C# 基类框架:Person 类及其子类 Student 和 Teacher

原文地址: https://www.cveoy.top/t/topic/n5fi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录