用c#定义一个学生 Student 类,有六个属性,分别为姓名、性别、年龄、语文、数学、英语成绩。 定义两个方法: (1)我叫 x , X 生,今年 X 岁了! (2)我叫 x ,我的总成绩是 X ,平均成绩是 X 。实例化两个对象并测试: (1)张三,男,20,三科成绩为:100,95,98 (2)小兰,女,18,三科成绩为:90,85,88
时间: 2024-03-15 07:47:59 浏览: 210
好的,以下是 C# 的代码实现:
```csharp
using System;
namespace StudentNamespace
{
class Student
{
private string name;
private string gender;
private int age;
private int chineseScore;
private int mathScore;
private int englishScore;
// 构造函数
public Student(string name, string gender, int age, int chineseScore, int mathScore, int englishScore)
{
this.name = name;
this.gender = gender;
this.age = age;
this.chineseScore = chineseScore;
this.mathScore = mathScore;
this.englishScore = englishScore;
}
// 方法1
public void Introduce()
{
Console.WriteLine("我叫{0},{1}生,今年{2}岁了!", name, gender, age);
}
// 方法2
public void ShowScore()
{
int totalScore = chineseScore + mathScore + englishScore;
double averageScore = totalScore / 3.0;
Console.WriteLine("我叫{0},我的总成绩是{1},平均成绩是{2:F2}。", name, totalScore, averageScore);
}
}
class Program
{
static void Main(string[] args)
{
Student student1 = new Student("张三", "男", 20, 100, 95, 98);
student1.Introduce();
student1.ShowScore();
Student student2 = new Student("小兰", "女", 18, 90, 85, 88);
student2.Introduce();
student2.ShowScore();
Console.ReadKey();
}
}
}
```
输出结果为:
```
我叫张三,男生,今年20岁了!
我叫张三,我的总成绩是293,平均成绩是97.67。
我叫小兰,女生,今年18岁了!
我叫小兰,我的总成绩是263,平均成绩是87.67。
```
希望能够帮助到您!
阅读全文