首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

急求相助,明天考试要用

2012-06-20 
急求帮助,明天考试要用。定义一个人类(Person类),该类中包含两个字段name,age。再定义一个学生类(Student类)

急求帮助,明天考试要用。
定义一个人类(Person类),该类中包含两个字段name,age。再定义一个学生类(Student类),它是Person类的派生类,该类中包含学号、英语成绩、数学成绩、语文成绩等字段。包含两个方法(方法名自定),一个方法的功能是计算总成绩并输出;另一个方法的功能是根据总成绩判断是否升学,总成绩>=200分时显示可以升学,否则显示不可升学。
在主函数中新建若干个student类的对象(四个以上),分别以学号,姓名,年龄,各科成绩作为参数。并分别调用上述的两个方法。


[解决办法]

C# code
using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {               Student x1 = new Student("1001", "学生1", 0x12, 90M, 91M, 92M);            Console.WriteLine("学生:{0} 总成绩:{1} 升学标志:{2}", x1.name, x1.计算总成绩().ToString(), x1.升学().ToString());            Student x2 = new Student("1002", "学生2", 0x12, 92M, 91M, 82M);            Console.WriteLine("学生:{0} 总成绩:{1} 升学标志:{2}", x2.name, x2.计算总成绩().ToString(), x2.升学().ToString());            Student x3 = new Student("1003", "学生3", 0x12, 90M, 71M, 92M);            Console.WriteLine("学生:{0} 总成绩:{1} 升学标志:{2}", x3.name, x3.计算总成绩().ToString(), x3.升学().ToString());            Student x4 = new Student("1004", "学生4", 0x12, 60M, 11M, 22M);            Console.WriteLine("学生:{0} 总成绩:{1} 升学标志:{2}", x4.name, x4.计算总成绩().ToString(), x4.升学().ToString());        }    }    public class Person    {        // Fields        private int _age;        private string _name;        // Properties        public int age        {            get            {                return this._age;            }            set            {                this._age = value;            }        }        public string name        {            get            {                return this._name;            }            set            {                this._name = value;            }        }    }    public class Student : Person{    // Fields    private decimal _数学成绩;    private string _学号;    private decimal _英语成绩;    private decimal _语文成绩;    // Methods    public Student(string 学号, string name, int age, decimal 数学成绩, decimal 语文成绩, decimal 英语成绩)    {               this.name = name;        this .age = age;        this.学号 = 学号;        this.数学成绩 = 数学成绩;        this.语文成绩 = 语文成绩;        this.英语成绩 = 英语成绩;            }    public decimal 计算总成绩()    {        decimal sum=0;        sum += this._数学成绩 + this._语文成绩 + this._英语成绩;        return   sum ;    }    public bool 升学()    {        return (decimal.Compare(this.计算总成绩(), 200M) >= 0);    }    // Properties    public decimal 数学成绩    {        get        {            return this._数学成绩;        }        set        {            this._数学成绩 = value;        }    }    public string 学号    {        get        {            return this._学号;        }        set        {            this._学号 = value;        }    }    public decimal 英语成绩    {        get        {            return this._英语成绩;        }        set        {            this._英语成绩 = value;        }    }    public decimal 语文成绩    {        get        {            return this._语文成绩;        }        set        {            this._语文成绩 = value;        }    }}}
[解决办法]
C# code
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace zy2{    class Person    {        public string Name { get; set; }        public int Age { get; set; }    }    class Student : Person    {        public string 学号 { get; set; }        public int 英语成绩 { get; set; }        public int 数学成绩 { get; set; }        public int 语文成绩 { get; set; }        private int 获得总分()        {            return this.英语成绩 + this.数学成绩 + this.语文成绩;        }        public void 显示总分()        {            Console.WriteLine("总分等于{0}.", 获得总分());        }        public void 升学判断()        {            if (获得总分() >= 200) Console.WriteLine("可以升学."); else Console.WriteLine("不可以升学.");        }        public Student(string name1, int age1, string 学号1, int 英语成绩1, int 数学成绩1, int 语文成绩1)        {             Name = name1;            Age = age1;             学号 = 学号1;             英语成绩 = 英语成绩1;            数学成绩 = 数学成绩1;             语文成绩 = 语文成绩1;        }    }    class Program    {        static void Main(String[] args)        {            Student[] students = new Student[]            {                new Student("张楠", 21, "1号", 86, 65, 96),                new Student("雪碧", 22, "2号", 70, 80, 76),                new Student("许涵", 21, "3号", 86, 90, 56),                new Student("尹平", 21, "4号", 65, 52, 86),                new Student("某某", 22, "5号", 30, 47, 27)            };            foreach (var item in students)            {                Console.WriteLine("学生姓名:{0}", item.Name);                item.显示总分();                item.升学判断();                Console.WriteLine();            }        }    }} 


[解决办法]

C# code
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace zy2{    class Person    {        public string Name { get; set; }        public int Age { get; set; }    }    class Student : Person    {        public string 学号 { get; set; }        public int 英语成绩 { get; set; }        public int 数学成绩 { get; set; }        public int 语文成绩 { get; set; }        public int 获得总分()        {            return this.英语成绩 + this.数学成绩 + this.语文成绩;        }        public bool 可以升学()        {            return 获得总分() >= 200;        }        public void 显示总分()        {            Console.WriteLine("总分等于{0}.", 获得总分());        }        public void 显示升学判断()        {            if (可以升学()) Console.WriteLine("可以升学."); else Console.WriteLine("不可以升学.");        }        public Student(string name1, int age1, string 学号1, int 英语成绩1, int 数学成绩1, int 语文成绩1)        {            Name = name1;            Age = age1;            学号 = 学号1;            英语成绩 = 英语成绩1;            数学成绩 = 数学成绩1;            语文成绩 = 语文成绩1;        }    }    class Program    {        static void Main(String[] args)        {            Student[] students = new Student[]            {                new Student("张楠", 21, "1号", 86, 65, 96),                new Student("雪碧", 22, "2号", 70, 80, 76),                new Student("许涵", 21, "3号", 86, 90, 56),                new Student("尹平", 21, "4号", 65, 52, 86),                new Student("某某", 22, "5号", 30, 47, 27)            };            foreach (var item in students)            {                Console.WriteLine("学生姓名:{0}", item.Name);                item.显示总分();                item.显示升学判断();                Console.WriteLine("{0}号{1}同学总分{2},{3}升学.", item.学号, item.Name, item.获得总分(), item.可以升学() ? "可以" : "不可以");                Console.WriteLine();            }        }    }}
[解决办法]
C# code
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace zy2{    class Person    {        public string Name { get; set; }        public int Age { get; set; }    }    class Student : Person    {        public string 学号 { get; set; }        public int 英语成绩 { get; set; }        public int 数学成绩 { get; set; }        public int 语文成绩 { get; set; }        public int 获得总分()        {            return this.英语成绩 + this.数学成绩 + this.语文成绩;        }        public bool 可以升学()        {            return 获得总分() >= 200;        }        public void 显示总分()        {            Console.Write("总分是{0},", 获得总分());        }        public void 显示升学判断()        {            if (可以升学()) Console.WriteLine("可以升学."); else Console.WriteLine("不可以升学.");        }        public Student(string name1, int age1, string 学号1, int 英语成绩1, int 数学成绩1, int 语文成绩1)        {            Name = name1;            Age = age1;            学号 = 学号1;            英语成绩 = 英语成绩1;            数学成绩 = 数学成绩1;            语文成绩 = 语文成绩1;        }    }    class Program    {        static void Main(String[] args)        {            Student[] students = new Student[]            {                new Student("张楠", 21, "1号", 86, 65, 96),                new Student("雪碧", 22, "2号", 70, 80, 76),                new Student("许涵", 21, "3号", 86, 90, 56),                new Student("尹平", 21, "4号", 65, 52, 86),                new Student("某某", 22, "5号", 30, 47, 27)            };            foreach (var item in students)            {                Console.Write("{0}号{1}同学", item.学号, item.Name);                item.显示总分();                item.显示升学判断();            }        }    }} 

热点排行