用c++实现创建学生类,并依据学生成绩来排序

1.题目:

创建一个对象数组,数组的元素是学生对象,学生的信息包括学号、姓名和成绩(一门课成绩),在main函数中将数组元素按学生成绩从小到大的顺序排序并显示出来。(学生对象不得少于5个,显示定义构造函数和析构函数,并在里面加上测试语句)

2.代码:

#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
  Student()
  {
      cout << "已经调用了无参函数!!!!!!!!"<<endl;
  }
  Student(string Name,string Num,int Score)
  {
      name = Name;
      num  = Num;
      score = Score;
      cout << "已经调用了含参构造函数!!!!" <<endl;
  }
  ~Student()
  {
      cout <<"已经调用了析构函数!!!"<<endl;
  }
  void stu_show()
  {
      cout << "学生的学号为:" << num << ",学号为:"  << name <<",成绩: " << score << endl;
  }
  int  get_score()
  {
      return score;
  }
  int  set_score()
  {
      cin >> score;
  }
private:
  string name;
  string num;
  int score;
};

int main()
{
    Student stu[5] = {
    Student ("2111030616","王五",100),
    Student ("2111030615","张三",99),
    Student ("2111030614","李四",60),
    Student ("2111030622","黄六",96),
    Student ("2111030658","胡七",50),
    };
    for (int i = 0; i < 4 ;++i )
        {
            for (int j = 0 ; j < 4 - i  ; ++j)
            {
                if(stu[j].get_score()> stu [j+1].get_score())
              {
               Student tmp;
               tmp = stu[j];
               stu[j] = stu[j+1];
               stu[j+1] = tmp;
              }
            }
        }

  for (int i = 0; i < 5; ++i )
  {
      stu[i].stu_show();
  }
    return 0;
}

3.结果:

 

  • 7
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: 可以这样创建一个学生对象数组: ``` class Student { constructor(id, name, score) { this.id = id; this.name = name; this.score = score; } } const students = [ new Student(1, '张三', 80), new Student(2, '李四', 90), new Student(3, '王五', 85), new Student(4, '赵六', 95), new Student(5, '钱七', 75) ]; ``` 其中,`Student` 是一个类,用来创建学生对象。每个学生对象有三个属性:`id` 表示学号,`name` 表示姓名,`score` 表示成绩。 `students` 是一个学生对象数组,包含了 5 个学生对象。 要求总人数和平均分,可以使用以下代码: ``` const total = students.length; const sum = students.reduce((acc, cur) => acc + cur.score, ); const average = sum / total; console.log(`总人数:${total},平均分:${average}`); ``` 其中,`total` 表示总人数,等于学生对象数组的长度;`sum` 表示所有学生的成绩之和,使用 `reduce` 方法实现;`average` 表示平均分,等于成绩之和除以总人数。最后使用 `console.log` 输出结果。 ### 回答2: 创建一个对象数组来存储学生对象,需要先定义一个学生类学生类中包含学号、姓名和成绩三个属性。在创建对象数组时,需要使用new关键字来实例化学生对象,同时将学号、姓名和成绩等信息传递给构造方法来初始化对象。 例如: ```java class Student { // 学生类 int id; // 学号 String name; // 姓名 double score; // 成绩 public Student(int id, String name, double score) { // 构造方法 this.id = id; this.name = name; this.score = score; } } ``` 然后,在主函数中创建学生对象数组,实例化对象并存储到数组中。 例如: ```java public static void main(String[] args) { Student[] students = new Student[5]; // 创建学生对象数组 students[0] = new Student(1001, "张三", 90); // 实例化对象并存储到数组中 students[1] = new Student(1002, "李四", 85); students[2] = new Student(1003, "王五", 92); students[3] = new Student(1004, "赵六", 88); students[4] = new Student(1005, "钱七", 95); } ``` 接下来,可以通过遍历学生对象数组来计算总人数和平均分。总人数可以使用数组的length属性来获取,平均分需要遍历数组,计算并累加所有学生的成绩,然后除以学生总数。 例如: ```java int count = students.length; // 学生总人数 double sum = 0.0; // 成绩总和 for (int i = 0; i < count; i++) { sum += students[i].score; // 累加成绩 } double average = sum / count; // 平均成绩 System.out.println("总人数:" + count); System.out.println("平均成绩:" + average); ``` 最终的完整代码如下: ### 回答3: 题目要求我们创建一个对象数组,并且该数组中的元素都是学生对象,学生对象包括学号、姓名和成绩的信息,要求至少有5个学生对象。我们需要计算这个数组中学生的总人数和平均分。 首先,我们需要创建一个学生类,表示一个学生的信息。学生类包含三个属性:学号、姓名和成绩。可以通过构造方法初始化学生对象的属性,同时需要提供相应的getter和setter方法来获取或设置学生的信息。例如: ```java public class Student { private String id; // 学号 private String name; // 姓名 private double score; // 成绩 public Student(String id, String name, double score) { this.id = id; this.name = name; this.score = score; } // getter和setter方法省略... } ``` 接着,在主函数中我们可以创建一个学生对象数组,例如: ```java Student[] students = new Student[5]; students[0] = new Student("001", "Tom", 80.0); students[1] = new Student("002", "Jack", 90.0); students[2] = new Student("003", "Mary", 85.0); students[3] = new Student("004", "Emma", 92.0); students[4] = new Student("005", "John", 88.0); ``` 以上代码创建了一个包含5个学生对象的数组。每个学生对象保存了学号、姓名和成绩信息。 接下来,我们需要计算学生的总人数和平均分。在Java中,可以使用数组的长度属性`length`来获取数组的元素个数。例如,另外定义两个变量`total`和`average`表示总分数和平均分数,计算方法如下: ```java int total = 0; for (int i = 0; i < students.length; i++) { total += students[i].getScore(); // 累加成绩 } double average = total / (double)students.length; ``` 以上代码先定义了一个`total`变量表示总分数,然后使用`for`循环遍历学生对象数组,累加每个学生的成绩。最后,使用`students.length`计算学生的总人数,然后将总分数除以总人数得到平均分数。 最终,我们可以将总人数和平均分数输出,例如: ```java System.out.println("总人数: " + students.length); System.out.println("平均分: " + average); ``` 总结:本题是一个典型的Java类与数组应用题,我们需要创建一个学生类来封装学生的信息,然后创建一个学生对象数组,并且使用for循环计算学生的总人数和平均分数。在解决本题的过程中,可以巩固Java类和数组的基本知识点,培养面向对象编程和计算机思维能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小捣蛋.

建议你还是留着你的小钱吧!!!

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

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

打赏作者

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

抵扣说明:

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

余额充值