一个c#代码
2021-06-28 13:06
标签:names 学生 stun repo 构造 etc 提示 rgs epo 注意对象引用使用前要进行实例化 一个c#代码 标签:names 学生 stun repo 构造 etc 提示 rgs epo 原文地址:https://www.cnblogs.com/huwt/p/10041742.htmlusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace experment5_1
{
class Program
{
//Student类包含学生的所有必要信息
//以及关于这些信息的计算方法,以及输出学生信息的方法
class Student
{
public string stuNumber; //学号
public string stuName; //姓名
public int[] scores; //分数
public int[] scorePoint; //分数对应的点数
public Course[] courses; //参加的课程
public double staGPA; //标准GPA
public double ordGPA; //一般GPA
public Student() { }
public Student(string name, string number) //构造函数重载
{
stuName = name;
stuNumber = number;
}
public void setScore(int[] score) //设置学生的分数
{
scores = new int[score.Length]; //实例化scores数组
for (int i = 0; i )
scores[i] = score[i];
}
public void setPoint() //设置学分点数
{
scorePoint = new int[scores.Length]; //实例化scorepoint数组
for (int i = 0; i )
if (scores[i] >= 90)
scorePoint[i] = 4;
else if (scores[i] >= 80)
scorePoint[i] = 3;
else if (scores[i] >= 70)
scorePoint[i] = 2;
else if (scores[i] >= 60)
scorePoint[i] = 1;
else
scorePoint[i] = 0;
}
public void setCourse(Course[] course) //设置学生参加的课程
{
courses = new Course[course.Length]; //实例化courses数组
for (int i = 0; i )
courses[i] = course[i];
}
public void calGPA() //计算标准GPA和常见GPA
{
//此函数中要使用scorePoint,所以在使用前要先调用setPoint()函数
//否则会提示未将对象引用实例化
setPoint();
int sumSta = 0;
int sumOrd = 0;
int sumCre = 0;
for (int i = 0; i )
{
sumSta += courses[i].couCredit * scorePoint[i];
sumOrd += scores[i] * courses[i].couCredit;
sumCre += courses[i].couCredit;
}
ordGPA = sumSta * 1.0 / sumCre;
staGPA = sumOrd * 4.0 / (sumCre * 100);
}
public void disStudent()
{
//要先调用calGPA()来计算GPA
calGPA();
Console.WriteLine("学号:{0}\t姓名:{1}", stuNumber, stuName);
Console.WriteLine("\t课程名\t学分\t分数");
for (int i = 0; i )
Console.WriteLine("\t{0}\t{1}\t{2}", courses[i].couName, courses[i].couCredit, scores[i]);
Console.WriteLine("常见算法GPA={0:n},标准算法GPA={1:n}", ordGPA, staGPA);
}
}
//Course类包含课程名称和对应的学分
class Course
{
public string couName;
public int couCredit;
public Course() { }
public Course(string name, int credit)
{
couCredit = credit;
couName = name;
}
}
static void Main(string[] args)
{
Student student = new Student("王华", "1");
Course[] courses = new Course[] { new Course("课程1", 4), new Course("课程2", 3), new Course("课程3", 2), new Course("课程4", 6), new Course("课程5", 3) };
int[] scores = new int[] { 92, 80, 98, 70, 89 };
student.setScore(scores);
student.setCourse(courses);
student.disStudent();
}
}
}
下一篇:win+ R下的常见命令