C# 学生信息管理控制台应用程序设计:小学生、中学生、大学生成绩统计
using System; using System.Collections.Generic;
namespace ConsoleApp1 { abstract class Student { protected string name; // 学生姓名 protected int age; // 学生年龄 public static int num = 0; // 班级学生人数,静态变量 public Student(string name, int age) { this.name = name; this.age = age; num++; } public string Name { get => name; } public virtual string GetStudentType() { return 'student'; } public abstract double Sum(); // 抽象方法 public string GetInfo() { return string.Format('Name:{0}, {1}, Age is {2}', name, GetStudentType(), age); } }
class Pupil : Student
{
protected double chinese; // 语文成绩
protected double math; // 数学成绩
public Pupil(string name, int age, double chinese, double math) : base(name, age)
{
this.chinese = chinese;
this.math = math;
}
public override string GetStudentType() { return 'pupil'; }
public override double Sum()
{
return Math.Round((chinese + math) / 2, 2); // 保留两位小数
}
public new string GetInfo() // 隐藏父类
{
return string.Format('{0}, AvgScore:{1:f2};', base.GetInfo(), Sum());
}
}
class Middle : Student
{
protected double chinese;
protected double math;
protected double english;
public Middle(string name, int age, double chinese, double math, double english) : base(name, age)
{
this.chinese = chinese;
this.math = math;
this.english = english;
}
public override string GetStudentType() { return 'middle school student'; }
public override double Sum()
{
return Math.Round((chinese + math + english) / 3, 2);
}
public new string GetInfo()
{
return string.Format('{0}, AvgScore:{1:f2};', base.GetInfo(), Sum());
}
}
class College : Student
{
protected double compulsoryCredits; // 必修课学分
protected double optionalCredits; // 选修课学分
public College(string name, int age, double compulsoryCredits, double optionalCredits) : base(name, age)
{
this.compulsoryCredits = compulsoryCredits;
this.optionalCredits = optionalCredits;
}
public override string GetStudentType() { return 'college student'; }
public override double Sum()
{
return Math.Round(compulsoryCredits + optionalCredits, 2);
}
public new string GetInfo()
{
return string.Format('{0}, TotalCredits :{1:f2};', base.GetInfo(), Sum());
}
}
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
string[] input;
while ((input = Console.ReadLine().Split())[0] != '0')
{
if (input.Length != 6 || !(input[0] == '1' || input[0] == '2' || input[0] == '3'))
continue;
string name = input[1];
int age = int.Parse(input[2]);
double a = double.Parse(input[3]);
double b = double.Parse(input[4]);
int type = int.Parse(input[0]);
if (type == 1)
students.Add(new Pupil(name, age, a, b));
else if (type == 2)
students.Add(new Middle(name, age, a, b, double.Parse(input[5])));
else if (type == 3)
students.Add(new College(name, age, a, b));
Console.WriteLine('Total number of student:{0}, {1}', Student.num, students[students.Count - 1].GetInfo());
}
}
}
}
原文地址: https://www.cveoy.top/t/topic/nYTJ 著作权归作者所有。请勿转载和采集!