定义一个Student类,包括姓名、学号、成绩三个成员变量以及getName()、getNo()、getScore()三个方法。在构

定义一个Student类,包括姓名、学号、成绩三个成员变量以及getName()、getNo()、getScore()三个方法。在构造函数中初始化姓名、学号、成绩三个成员变量。定义一个大小为20的类型为Student的数组,按学生成绩的高低,依次输出姓名和成绩。

//参考程序:

#include "iostream"

#include "string"

using namespace std;

class Student

{

private:

int no; //学号

string name; //姓名

int score; //成绩

public:

Student()

{ }

Student(int no, string name, int score)

{

this->no = no;

this->name = name;

this->score = score;

}

string getName()

{

return name;

}

int getNo()

{

return no;

}

int getScore()

{

return score;

}

void display()

{

cout<<no<<"\t"<<name<<"\t"<<score<<endl;

}

};

void sort(Student stus[], int len)

{

int i, j, k;

Student temp;

for(i=0; i<len-1; i++)

{

k = i;

for(j=i+1; j<len; j++)

if(stus[j].getScore() > stus[k].getScore())

k = j;

if(k != i)

{

temp = stus[k];

stus[k] = stus[i];

stus[i] = temp;

}

}

}

void main()

{

int i;

int len = 5;

Student stus[20];

int no;

string name;

int score;

cout<<"请输入20个学生的信息(学号、姓名、成绩) : "<<endl;

for(i=0; i<len; i++)

{

cout<<"No. "<<i+1<<" : ";

cin>>no>>name>>score;

stus[i] = Student(no, name, score);

}

sort(stus, len);

cout<<"学生信息一览(按成绩降序) : "<<endl;

for(i=0; i<len; i++)

{

stus[i].display();

}

}

运行结果:

追问

你好 可不可以用java语言呢,谢谢!!!

追答

早说嘛,我看到你的提问分类是C/C++:

import java.util.Scanner;

class Student
{
private int no; //学号
private String name; //姓名
private int score; //成绩

public Student()
{ }

public Student(int no, String name, int score)
{
this.no = no;
this.name = name;
this.score = score;
}

public String getName()
{
return name;
}

public int getNo()
{
return no;
}

public int getScore()
{
return score;
}

public void display()
{
System.out.println(no + "\t" + name + "\t" + score);
}
}

public class TestStudent {
public static void main(String[] args) {
int i;
int len = 20; //学生人数
Student[] stus = new Student[len];
int no;
String name;
int score;
Scanner scan = new Scanner(System.in);

System.out.println("请输入20个学生的信息(学号、姓名、成绩) : ");
for(i=0; i stus[k].getScore())
k = j;
if(k != i)
{
temp = stus[k];
stus[k] = stus[i];
stus[i] = temp;
}
}
}
}

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答