6-3 学生类 (20 分)

题目描述

有一个学生类的结构如下:
class Student { private int no; private String name; private int score; public Student(int _no, String _name, int _score) { no = _no; name = _name; score = _score; } public int getNo() {return no;} public String getName() {return name;} public int getScore() {return score;} public void print(){ System.out.println(no + " “+name+” "+score); } }
请构造main函数完成如下功能: 从键盘中读入三个学生的信息,比较他们的成绩,按照成绩由高到低排列输出

输入描述:

三个学生的学号、姓名、成绩

输出描述: 由高到低排列输

出的三个学生信息

裁判测试程序样例:

/*你的代码被嵌在这里*/

class Student {
	private int no;
	private String name;
        private int score;
	
	public Student(int _no, String _name, int _score) {
		no = _no;
		name = _name;
                score = _score;
	}
        public int getNo() {return no;}
        public String getName() {return name;}
        public int getScore() {return score;}
	
	public void print(){
		System.out.println(no + " "+name+" "+score);
	}
}

输入样例:

在这里给出一组输入。例如:

1 wang 89
2 liu 78
3 ma 90

输出样例:

在这里给出相应的输出。例如:

3 ma 90
1 wang 89
2 liu 78

参考答案

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		final int N = 3; // 学生个数
		Scanner cin = new Scanner(System.in);
		Student[] stu = new Student[N];
		
		for(int i = 0; i < N; i ++ ) {
			stu[i] = new Student(cin.nextInt(), cin.next(), cin.nextInt());
		}
		
		Arrays.sort(stu, new Comparator<Student>( ) { // 使用 Comparator 建立以什么属性进行排序
			@Override
			public int compare(Student o1, Student o2) {
				// TODO Auto-generated method stub
				return o2.getScore() - o1.getScore();
			}
		});
		
		for(int i = 0; i < N; i ++) {
			stu[i].print();
		}
	}
}

好的,我来回答你的问题。 面向对象的编程思想可以很好的应用到学生类的构建学生类可以包括以下属性: 1. 姓名 2. 学号 3. 年龄 4. 性别 5. 班级 6. 成绩,姓名、学号、年龄、性别和班级可以定义为类的员变量,成绩可以定义为类的函数。 在实现学生类时,需要定义构造函数和析构函数函数等。例如,可以定义一个函数来计算学生的平均成绩,还可以定义一个函数来输出学生信息等。 下面是一个简单的C++代码示例: ```cpp #include <iostream> #include <string> using namespace std; class Student { private: string name; string id; int age; char gender; string clazz; public: Student(string n, string i, int a, char g, string c) { name = n; id = i; age = a; gender = g; clazz = c; } ~Student() {} void printInfo() { cout << "姓名:" << name << endl; cout << "学号:" << id << endl; cout << "年龄:" << age << endl; cout << "性别:" << gender << endl; cout << "班级:" << clazz << endl; } float getAverageScore(float s1, float s2, float s3) { return (s1 + s2 + s3) / 3; } }; int main() { Student stu("张三", "20210001", 20, 'M', "计算机科学与技术"); stu.printInfo(); float score1 = 90, score2 = 85, score3 = 95; float avg = stu.getAverageScore(score1, score2, score3); cout << "平均成绩:" << avg << endl; return 0; } ``` 以上代码定义了一个名为Student的类,包含了姓名、学号、年龄、性别和班级这些员变量,以及打印学生信息和计算平均成绩函数。在main函数,实例化了一个学生对象,并调用了printInfo和getAverageScore函数。 希望这个例子能够帮助你更好地理解面向对象编程思想在学生类构建的应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Hello Spring

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值