6-3 学生数统计 (20 分)

构造类Student,包含姓名,性别,年龄。提供必要的构造函数和其他成员函数。

提供静态函数getMaleCount,getFemaleCount,能够获得所有在main函数中构造的Student对象中男生和女生的数量。

main函数中先读取学生个数n,而后构造n个学生对象,最后分别打印其中男生和女生的人数。(输入的三个字段用空格分开,名字内部没有空格,性别用数字表示,1为男生,0为女生)

裁判测试程序样例:

//在这里给出函数被调用进行测试的例子。例如:
import java.util.Scanner;
public class Main{
 public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
     int n = sc.nextInt();
     for(int i=0;i<n;i++){
         Student s = new Student(sc.next(),sc.nextInt(), sc.nextInt());
     }
     System.out.println("number of male students:" + Student.getMaleCount() );
     System.out.println("number of female students:" + Student.getFemaleCount() );
 }
}

/* 请在这里填写答案 */

输入样例:

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

5
LiuMing 0 20
FangDa 1 19
DaShan 0 20
ChenHuang 0 21
DeLi 0 20

输出样例:

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

number of male students:1
number of female students:4

代码示例:

class Student
{
    public String name;
    public int sex;
    public int age;
    static int malenumber=0;
    static int femalenumber=0;
    public Student(String _name,int _sex,int _age)
    {
        this.name=_name;
        this.sex=_sex;
        this.age=_age;
        if(_sex==1)
            malenumber++;
        if(_sex==0)
            femalenumber++;
    }
    public static int getMaleCount()
    {
        return malenumber;
    }
    public static int getFemaleCount()
    {
        return femalenumber;
    }
}
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Student.h #include <string> using namespace std; class Student { private: string id; string name; char gender; int score1, score2, score3, score4; static int count; static int totalScore; static int highestScore; static int lowestScore; public: Student(string id, string name, char gender); ~Student(); void updateInfo(); void printInfo(); void searchBy(string keyword); static void calcAvgScore(); static void calcHighAndLowScore(); }; Student.cpp #include <iostream> #include "Student.h" using namespace std; int Student::count = 0; int Student::totalScore = 0; int Student::highestScore = 0; int Student::lowestScore = 0; Student::Student(string id, string name, char gender) { this->id = id; this->name = name; this->gender = gender; score1 = score2 = score3 = score4 = 0; count++; } Student::~Student() { cout << "本程序由" << name << "编写!" << endl; } void Student::updateInfo() { cout << "请输入 " << name << " 的成绩:" << endl; cout << "课程1成绩:"; cin >> score1; cout << "课程2成绩:"; cin >> score2; cout << "课程3成绩:"; cin >> score3; cout << "课程4成绩:"; cin >> score4; totalScore += score1 + score2 + score3 + score4; highestScore = max(highestScore, max(score1, max(score2, max(score3, score4)))); lowestScore = min(lowestScore, min(score1, min(score2, min(score3, score4)))); } void Student::printInfo() { cout << "学号:" << id << " 姓名:" << name << " 性别:" << gender << " 成绩:" << score1 << " " << score2 << " " << score3 << " " << score4 << endl; } void Student::searchBy(string keyword) { if (id == keyword || name == keyword) { printInfo(); } } void Student::calcAvgScore() { cout << "所有学生的课程平均成绩为:" << (double)totalScore / (count * 4) << endl; } void Student::calcHighAndLowScore() { cout << "所有学生的最高为:" << highestScore << endl; cout << "所有学生的最低为:" << lowestScore << endl; } main.cpp #include <iostream> #include "Student.h" using namespace std; int main() { int n; do { cout << "请输入学生(2-6):"; cin >> n; } while (n < 2 || n > 6); Student* students[n]; string id, name; char gender; for (int i = 0; i < n; i++) { cout << "请输入第" << i + 1 << "个学生的信息:" << endl; cout << "学号:"; cin >> id; cout << "姓名:"; cin >> name; cout << "性别:"; cin >> gender; students[i] = new Student(id, name, gender); } for (int i = 0; i < n; i++) { students[i]->updateInfo(); } Student::calcAvgScore(); Student::calcHighAndLowScore(); string keyword; cout << "请输入要查询的学生的学号或姓名:"; cin >> keyword; for (int i = 0; i < n; i++) { students[i]->searchBy(keyword); } for (int i = 0; i < n; i++) { students[i]->printInfo(); } for (int i = 0; i < n; i++) { delete students[i]; } return 0; }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值