| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 760 人关注过本帖
标题:请大家看下这段程序哪里不对了呢?
只看楼主 加入收藏
zjm_sunrise
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-7-25
收藏
 问题点数:0 回复次数:5 
请大家看下这段程序哪里不对了呢?
这样一个程序设计题:编写出一个通用的人员类(Person),该类具有姓名(Name)、年龄(Age)、性别(Sex)等域。然后通过对Person类的继承得到一个学生类(Student),该类能够存放学生的5门课的成绩,并能求出平均成绩,要求对该类构造函数进行重载,至少给出三个形式。最后编程对Student类的功能进行验证。
按照要求运行界面设计如图所示编写的程序如下:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace stuaver
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public class Person
        {
            public string Name;
            public int Age;
            public string Sex;
        }
        public class Student : Person
        {
            public Student(string n, int a, string  s)//构造函数一
            {
                Name = n;
                Age = a;
                Sex = s;
            }
            public Student()
            {
                Name = "";
                Age = 0;
                Sex = "";
            }
            
            public double Aver()
            {
                int i;
                int[] a = new int[5];
                double aver = 0;
                Random randomObj = new Random();
                for (i = 0; i < 5; i++)
                {
                    a[i] = randomObj.Next(10, 100);
                    aver += a[i];
                    aver /= 5;                     
                }
                return (aver);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void label4_Click(object sender, EventArgs e)
        {
            string n;
            int a;
            string s;
            n = textBox2.Text;
            a = Convert.ToInt32(textBox3.Text);
            s =textBox4.Text;
            Student Stu = new Student(n, a, s);
            textBox1.Text = Convert.ToString(Stu.Aver());
        }
    }
}
才忘记存在的说问题了,这段程序编译运行都可以顺利通过,但得不到任何运行结果,请大家帮忙给看下罢;另外,要求对Student类给出至少三个构造函数形式进行重载,但我只想出两个,大家看看还有什么的构造函数可以写呢……
搜索更多相关主题的帖子: 界面设计 程序设计 如图所示 通用 姓名 
2008-07-28 22:50
kevin87
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2008-7-26
收藏
得分:0 
你这个实在是看的太乱了,首先你的思路就没有理清!发个代码给你参考下
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication7
{
    class Square : Shape    //Square类继承自Shape类
    {
        double _radius;
        double _sideLen;
        public override void getArea()
        {
            for (int i = 0; i < 1; i++)
            {
                Console.WriteLine("Shape(基类)的(抽象方法)getArea()被Square(派生类)重写");
                Console.WriteLine("请输入矩形的长");
                _radius = double.Parse(Console.ReadLine());
                for (int j =0; j < 1; j++)
                {
                    Console.WriteLine("请输入矩形的宽");
                    _sideLen = double.Parse(Console.ReadLine());
                }
            }
             double area = _radius * _sideLen;
             Console.WriteLine("正方形的面积为:{0}", area + "\n\n\n");
        }
        }
    }


using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication7
{
     class Circle : Shape  //Circle类继承自Shape类
    {
        double   _radius;
        const double _sideLen = 3.14;
         public override void getArea()
         {
            Console.WriteLine("Shape(基类)的(抽象方法)getArea()被Circle(派生类)重写");
           Console.WriteLine("请输入圆形的半径");
           _radius = double.Parse(Console.ReadLine());
           double _area = (_radius * _radius) * _sideLen;
           Console.WriteLine("圆形面积为:{0}", _area + "\n\n\n");
       }
    }
}

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication7
{
    //父类
    abstract class Shape
    {
        string _color;
        public void getColor()
        {
            Console.WriteLine("输入颜色名称");
            _color = Console.ReadLine();
            Console.WriteLine("这个基类的getColor()方法,被派生类所继承,{0}", _color + "\n");
        }
        public abstract void getArea();
     }
}

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            Circle cobj = new Circle();
            cobj.getColor();
            cobj.getArea();
            Square sobj = new Square();
            sobj.getColor();
            sobj.getArea();
            Console.ReadLine();
        }
    }
}
2008-07-28 23:54
GabrielCNMao
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-7-1
收藏
得分:0 
for (i = 0; i < 5; i++)
                {
                    a[i] = randomObj.Next(10, 100);
                    aver += a[i];
                    aver /= 5;                     
                }
??
你這個是算平均成績嗎?aver/=5是不是應該放到循環外面呢?
2008-07-29 09:17
GabrielCNMao
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-7-1
收藏
得分:0 
構造函數隨便寫,只要參數不一樣就行。
2008-07-29 09:19
GabrielCNMao
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-7-1
收藏
得分:0 
代碼出錯更簡單,F10自己調調就出來了。
2008-07-29 09:20
KingTam
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2008-7-29
收藏
得分:0 
学生不是应该有一个唯一的ID(或者叫学号)的吗?以这个ID作为参数建一个构造函数可以吗?
2008-07-29 09:37
快速回复:请大家看下这段程序哪里不对了呢?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018985 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved