请大家看下这段程序哪里不对了呢?
这样一个程序设计题:编写出一个通用的人员类(Person),该类具有姓名(Name)、年龄(Age)、性别(Sex)等域。然后通过对Person类的继承得到一个学生类(Student),该类能够存放学生的5门课的成绩,并能求出平均成绩,要求对该类构造函数进行重载,至少给出三个形式。最后编程对Student类的功能进行验证。按照要求运行界面设计如图所示编写的程序如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
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类给出至少三个构造函数形式进行重载,但我只想出两个,大家看看还有什么的构造函数可以写呢……
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();
}
}
} for (i = 0; i < 5; i++)
{
a[i] = randomObj.Next(10, 100);
aver += a[i];
aver /= 5;
}
??
你這個是算平均成績嗎?aver/=5是不是應該放到循環外面呢? 構造函數隨便寫,只要參數不一樣就行。 代碼出錯更簡單,F10自己調調就出來了。 学生不是应该有一个唯一的ID(或者叫学号)的吗?以这个ID作为参数建一个构造函数可以吗?
页:
[1]
