6-3 学生类 (20分)

6-3 学生类 (20分)

输入描述:
三个学生的学号、姓名、成绩

输出描述:
由高到低排列输出的三个学生信息

	package test;
	import java.util.Scanner;
	public class Test {
	public static void main(String[] args) {
		Scanner out = new Scanner(System.in);
		Student a=new Student(out.nextInt(),out.next(),out.nextInt());
		Student b=new Student(out.nextInt(),out.next(),out.nextInt());
		Student c=new Student(out.nextInt(),out.next(),out.nextInt());
		if(a.getScore() < b.getScore() && b.getScore() < c.getScore())
		{
			c.print();
			b.print();a.print();
		}
		else if(c.getScore()>a.getScore() && a.getScore() > b.getScore())
		{
			c.print();a.print();b.print();
		}
		else if(b.getScore()>a.getScore() && a.getScore()> c.getScore())
		{
			b.print();a.print();c.print();
		}
		else if(b.getScore()>c.getScore() && c.getScore() > a.getScore())
		{
			b.print();c.print();a.print();
		}
		else if (a.getScore()>b.getScore() && b.getScore() > c.getScore())
		{
			a.print();b.print();c.print();
		}
		else if(a.getScore() > c.getScore() && c.getScore() > b.getScore())
			{
				a.print();c.print();b.print();
			}
			}
}
	/*你的代码被嵌在这里*/
	**/*裁判代码*/**


	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. 姓名 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, &#39;M&#39;, "计算机科学与技术"); 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值