创建一个对象数组,数组的元素是学生对象,学生的信息包括学号,姓名和成绩,在main函数种将所有成绩大于80分的学生的信息显示出来
时间: 2024-01-04 16:03:08 浏览: 91
以下是Python代码实现:
```python
class Student:
def __init__(self, id, name, score):
self.id = id
self.name = name
self.score = score
students = [
Student('001', 'Alice', 90),
Student('002', 'Bob', 75),
Student('003', 'Charlie', 85),
Student('004', 'David', 92),
Student('005', 'Emily', 78),
]
for student in students:
if student.score > 80:
print(f"学号:{student.id},姓名:{student.name},成绩:{student.score}")
```
输出结果:
```
学号:001,姓名:Alice,成绩:90
学号:003,姓名:Charlie,成绩:85
学号:004,姓名:David,成绩:92
```
相关问题
c++创建一个对象数组,数组的元素是学生对象,学生的信息包括学号,姓名和成绩,在main函数种将所有成绩大于80分的学生的信息显示出来
#include <iostream>
using namespace std;
class Student {
public:
int id;
string name;
float score;
};
int main() {
Student students[5] = {
{101, "张三", 78},
{102, "李四", 85},
{103, "王五", 92},
{104, "赵六", 74},
{105, "钱七", 80}
};
for (int i = 0; i < 5; i++) {
if (students[i].score > 80) {
cout << "学号:" << students[i].id << ",姓名:" << students[i].name << ",成绩:" << students[i].score << endl;
}
}
return 0;
}
c++创建一个对象数组,数组的元素是学生对象,学生的信息包括学号,姓名和成绩,在main函数种将所有成绩大于80分的学生的信息显示出来
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
int num;
string name;
int score;
Student(int num, string name, int score) {
this->num = num;
this->name = name;
this->score = score;
}
};
int main() {
Student students[] = {
Student(1, "Tom", 85),
Student(2, "Jerry", 75),
Student(3, "Mary", 90),
Student(4, "John", 80),
Student(5, "Lisa", 95)
};
int len = sizeof(students) / sizeof(Student);
for (int i = 0; i < len; i++) {
if (students[i].score > 80) {
cout << "学号:" << students[i].num << ",姓名:" << students[i].name << ",成绩:" << students[i].score << endl;
}
}
return 0;
}
阅读全文
相关推荐
















