struct Student
{
string name;
int[] score;
public Student(string name,int[] score)
{
//指向自身的指针
this.name = name;
this.score = score;
}
//计算平均分数
public float avg()
{
float num = 0.0f;
for (int i = 0; i < score.Length; i++)
{
num += score[i];
}
return num / score.Length;
}
public string Name
{
get { return name; }
}
public int[] Score
{
get { return score; }
set { score = value; }
}
}
namespace ConsoleApplication3
{
class StudentTest
{
public static void Main()
{
Student[] stu = new Student[3];
string[] name ={ "jack", "mary", "kate" };
int[] s ={ 80, 80, 80 };
for (int i = 0; i < 3; i++)
{
stu[i] = new Student(name[i], s);
Console.WriteLine("{0,10}{1,3}",stu[i].Name,stu[i].avg());
}
}
}
}谢谢各位了 拜托!
对于结构 真的不是很懂 ……
{
string name;
int[] score;
public Student(string name,int[] score)
{
//指向自身的指针
this.name = name;
this.score = score;
}
//计算平均分数
public float avg()
{
float num = 0.0f;
for (int i = 0; i < score.Length; i++)
{
num += score[i];
}
return num / score.Length;
}
public string Name
{
get { return name; }
}
public int[] Score
{
get { return score; }
set { score = value; }
}
}
namespace ConsoleApplication3
{
class StudentTest
{
public static void Main()
{
Student[] stu = new Student[3];
string[] name ={ "jack", "mary", "kate" };
int[] s ={ 80, 80, 80 };
for (int i = 0; i < 3; i++)
{
stu[i] = new Student(name[i], s);
Console.WriteLine("{0,10}{1,3}",stu[i].Name,stu[i].avg());
}
}
}
}谢谢各位了 拜托!
对于结构 真的不是很懂 ……
Student[] stu = new Student[3];//初始化学生结构实例
string[] name ={ "jack", "mary", "kate" };
int[] s ={ 80, 80, 80 };
for (int i = 0; i < 3; i++)
{
stu[i] = new Student(name[i], s);//循环将name数组里的名称与分数初始化
Console.WriteLine("{0,10}{1,3}",stu[i].Name,stu[i].avg());//输出学生名,与这个学生的平均分
}
{
string name;
int[] score;
public Student(string name,int[] score)//构造函数
{
//指向自身的指针
this.name = name;
this.score = score;
}
//计算平均分数
public float avg()
{
float num = 0.0f;
for (int i = 0; i < score.Length; i++)
{
num += score[i];
}
return num / score.Length;
}
public string Name
{
get { return name; }//获取字段值
}
public int[] Score//设置获取字段值
{
get { return score; }
set { score = value; }
}
}
namespace ConsoleApplication3
{
class StudentTest
{
public static void Main()
{
Student[] stu = new Student[3];
string[] name ={ "jack", "mary", "kate" };
int[] s ={ 80, 80, 80 };
for (int i = 0; i < 3; i++)
{
stu[i] = new Student(name[i], s);//struct变量
Console.WriteLine("{0,10}{1,3}",stu[i].Name,stu[i].avg());
}
}
}
}
{
get { return name; }
}
public int[] Score
{
get { return score; }
set { score = value; }
}属性的作用是什么呀??stu[i] = new Student(name[i], s) 里面的两个参数 怎么可以这样写呢??而且我的是“结构” 怎么在实例化的时候 是数组呢???
{
string name;声明下学生姓名
int[] score;学生成绩
public Student(string name,int[] score) 建立一个学生类
{
//指向自身的指针
this.name = name;
this.score = score;
}
//计算平均分数
public float avg()
{
float num = 0.0f;
for (int i = 0; i < score.Length; i++)把每个同学分数都加起来
{
num += score[i];
}
return num / score.Length;
返回学生平均成绩
}
public string Name
{
get { return name; }
}
public int[] Score
{
get { return score; }
set { score = value; }
}
}
namespace ConsoleApplication3
{
class StudentTest
{
public static void Main()
{
Student[] stu = new Student[3];
string[] name ={ "jack", "mary", "kate" };
int[] s ={ 80, 80, 80 };
for (int i = 0; i < 3; i++)
{
stu[i] = new Student(name[i], s);
Console.WriteLine("{0,10}{1,3}",stu[i].Name,stu[i].avg());通过控制台打印出来这三个同学的姓名以及平均分
}
}
}}
其实你慢慢分析,就是一个学生信息,然后输入学生成绩,求出学生的平均分,就这样
你TMD 没碰见过不懂啊!有种 你不学 进微软啊!