从键盘上获取名字、语文、数学的成绩,计算总分然后填充到Student对象中,并将对象写入文件...

本文介绍了一个简单的学生信息管理系统,该系统使用Java实现,能够录入学生的姓名和成绩,并将这些信息保存到文件中。此外,系统还提供了成绩统计和排序功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是源文件
Students.java文件

package com.kingsoft.main;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Students implements Serializable {
String name;
int[] record = new int[4];
int total;
float avg;
String grade;

public Students() {
}

public String getName() {
return name;
}

public int[] getRecord() {
return record;
}

public int getTotal() {
int s = 0;
for (int i = 0; i < record.length; i++)
s += record[i];
return s;
}

public float getAvg() {
float f = getTotal() / 4f;
return f;
}

public String getGrade() {
String str;
if (avg < 100 && avg > 90)
str = "A";
else if (avg > 80)
str = "B";
else if (avg > 70)
str = "C";
else if (avg > 60)
str = "D";
else
str = "F";
return str;
}
}


Students1.java文件

package com.kingsoft.main;

import java.io.*;

public class Students1 {
public static int s = 0;

/** Creates a new instance of Students1 */
public Students1() {
}

public static void main(String[] args) throws Exception {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("请顺序输入名字及国语,英语,数学,科学的成绩.(eof:输入完毕)");
String data;
String[] str = new String[5];
FileOutputStream fos = new FileOutputStream("C:\\Documents and Settings\\tliu\\桌面\\新建文件夹\\xi.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
while (true) {
data = br.readLine();
s++;
if (data.equals("eof"))
break;
str = data.split(" ");
Students stu = new Students();
stu.name = str[0];
System.out.println(stu.name);
stu.record[0] = Integer.parseInt(str[1]);
System.out.println(stu.record[0]);
stu.record[1] = Integer.parseInt(str[2]);
stu.record[2] = Integer.parseInt(str[3]);
stu.record[3] = Integer.parseInt(str[4]);
stu.total = stu.getTotal();
stu.avg = stu.getAvg();
stu.grade = stu.getGrade();
oos.writeObject(stu);
}
oos.close();
System.out.println("文件内容");
FileInputStream fis = new FileInputStream("D:\\xi.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
for (int j = 1; j < s; j++) {
Students stud;
stud = (Students) ois.readObject();
System.out.println("Students对象" + j + " " + "{" + stud.name + " " + stud.record[0] + " " + stud.record[1] + " "
+ stud.record[2] + " " + stud.record[3] + " " + stud.total + " " + stud.avg + " " + stud.grade + "}");
}
ois.close();
}
}

class Students2 {
public Students2() {
}

public static void main(String[] args) throws Exception {
// TODO code application logic here
int d = Students1.s;
int[] a = new int[d];
FileInputStream fis = new FileInputStream("C:\\Documents and Settings\\tliu\\桌面\\新建文件夹\\xi.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Students[] students = new Students[d];
for (int i = 1; i < d; i++) {
students[i] = (Students) ois.readObject();
}
ois.close();
System.out.println("名字 " + "国语 " + "英语 " + "数学 " + "科学 " + "总分 " + "平均分 " + "学分 " + "顺序");
for (int i = 1; i < d; i++) {
int max = i;
for (int j = i; j < d; j++) {
if (students[j].avg > students[i].avg)
max = j;
}
System.out.println(students[max].name + " " + students[max].record[0] + " " + students[max].record[1] + " "
+ students[max].record[2] + " " + students[max].record[3] + " " + students[max].total + " " + students[max].avg
+ " " + students[max].grade + " " + i);
}
}
}
#include <iostream> #include <iomanip> #include <fstream> using namespace std; // 定义学生结构体 class Student { int id; string name; int chinese; int math; int english; int total; public: bool operator>(Student& s) { return total > s.total; }// 按总成绩从高到低排序要用到 void calculateTotal() { total = chinese + math + english; } friend ostream& operator<<(ostream& out, Student& stu); friend istream& operator>>(istream& in, Student& stu); friend ifstream& operator>>(ifstream& input, Student& stu); friend void sort(Student* stus, int n); }; /***********begin************/ ifstream& operator>>(ifstream& input, Student& stu) { input >> stu.id >> stu.name >> stu.chinese >> stu.math >> stu.english; return input; } istream& operator>>(istream& in, Student& stu) { in >> stu.id>>stu.name>>stu.chinese>>stu.math>>stu.english; return in; } ostream& operator<<(ostream& out, Student& stu) { out << stu.id <<' '<< stu.name << ' ' << stu.chinese << ' ' << stu.math << ' ' << stu.english <<' '<<stu.total<< endl; return out; } void sort(Student* stus, int n) { for (int a = 0;a < n;a++) { for (int b = a + 1;b < n;b++) { if (stus[a].total < stus[b].total) { Student temp = stus[a]; stus[a] = stus[b]; stus[b] = temp; } } } } /***********end************/ int main() { // 1. 输入学生信息写入文件 result.dat ofstream outFile("result.txt"); if (!outFile) { cerr << "无法打开文件 result.txt" << endl; return 1; } int n; cin >> n; /***********创建动态数组stus************/ Student* stus = new Student[n]; for (int i = 0; i < n; i++) { cin >> stus[i]; stus[i].calculateTotal(); outFile << stus[i]; /***********保存学生信息到文件result.txt************/ } outFile.close(); // 2. 从 result.txt 中读取数据排序 ifstream inFile("result.txt"); if (!inFile) { cerr << "无法打开文件 result.txt" << endl; return 1; } int i = 0; while ( inFile>>stus[i] /***********从文件result.txt读取学生信息存入stus[i]************/) { i++; } inFile.close(); /***********调用前面定义的sort函数对数组stus的信息按总分排序************/ sort(stus,n); // 3. 将排序后的数据写入文件 sort.txt,输出到屏幕 ofstream sortedFile("sort.txt"); if (!sortedFile) { cerr << "无法打开文件 sort.txt" << endl; return 1; } for (i = 0; i < n;i++) { sortedFile << stus[i]; cout << stus[i]; } sortedFile.close(); delete[]stus; return 0; }
最新发布
03-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值