用C#撰寫出一個通用的人員類(Person),該類具有姓名(Name)、年齡(Age)、性別(Sex)等屬性。然后對Person 類的繼承得到一個學生類(Student),Student類能夠存放5門課的成績,并能求出平均成績。最后在Main函式中對Student類的功能進行驗證
uj5u.com熱心網友回復:
不如 退學uj5u.com熱心網友回復:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
}
public class Student : Person
{
/// <summary>
/// 語文分數
/// </summary>
public int ChineseScore { get; set; }
/// <summary>
/// 數學分數
/// </summary>
public int MathScore { get; set; }
/// <summary>
/// 英語分數
/// </summary>
public int EnglishScore { get; set; }
/// <summary>
/// 物理分數
/// </summary>
public int PhysicsScore { get; set; }
/// <summary>
/// 化學分數
/// </summary>
public int ChemistryScore { get; set; }
}
static void Main(string[] args)
{
Student li = new Student();
li.Name = "李四";
li.Age = 18;
li.Sex = "男";
li.ChineseScore = 91;
li.MathScore = 85;
li.EnglishScore = 48;
li.PhysicsScore = 95;
li.ChemistryScore = 85;
Console.WriteLine(String.Format("學生姓名:{0},性別:{1},年齡:{2}",li.Name,li.Sex,li.Age));
Console.WriteLine(String.Format("語文成績:{0},數學成績:{1},英語成績:{2},物理成績:{3},化學成績:{4}", li.ChineseScore, li.MathScore, li.EnglishScore, li.PhysicsScore, li.ChemistryScore));
double avgScore = (li.ChineseScore + li.MathScore + li.EnglishScore + li.PhysicsScore + li.ChemistryScore) / 5.0;
Console.WriteLine(String.Format("學生姓名:{0},五科平均分:{1}", li.Name, avgScore));
Console.ReadKey();
}
uj5u.com熱心網友回復:
果然還是有好心人給做作業
uj5u.com熱心網友回復:
太謝謝了,我剛剛自己也實作了一個。
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace task4{ class Person { public Person(string name, string age, string sex) { this.Name = name; this.Age = age; this.Sex = sex; } public String Name { get; set; } public string Age { get; set; } public String Sex { get; set; } } class Student : Person { public Student(string name, string age, string sex) : base(name, age, sex) { Console.Write("{0}的語文成績為:", Name); int str4 = int.Parse(Console.ReadLine()); Grade = str4; Console.Write("{0}的數學成績為:", Name); int str = int.Parse(Console.ReadLine()); Grade1 = str; Console.Write("{0}的英語成績為:", Name); int str1 = int.Parse(Console.ReadLine()); Grade2 = str1; Console.Write("{0}的物理成績為:", Name); int str2 = int.Parse(Console.ReadLine()); Grade3 = str2; Console.Write("{0}的化學成績為:", Name); int str3 = int.Parse(Console.ReadLine()); Grade4 = str3; } public int Grade { get; set; } public int Grade1 { get; set; } public int Grade2 { get; set; } public int Grade3 { get; set; } public int Grade4 { get; set; } public void Avg() { int sum = 0; int avg = 0; sum = Grade + Grade1 + Grade2 + Grade3 + Grade4; avg = sum / 5; Console.WriteLine("他的平均成績為{0}", avg); } } class Program { static void Main(string[] args) { Console.Write("學生姓名:"); string Name = Console.ReadLine(); Console.Write("學生年齡:"); string Age = Console.ReadLine(); Console.Write("學生性別:"); string Sex = Console.ReadLine(); Student student1 = new Student(Name, Age, Sex); student1.Avg(); } }}
uj5u.com熱心網友回復:
我也妹想到啊,第一次,發帖。
uj5u.com熱心網友回復:
好多雷鋒
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/119052.html
標籤:C#
