主类
import java.util.*;
public class Main{
public static void main(String[] args){
ArrayList<student> students=new ArrayList<>();
IO.in(students);
int number;
A:while(true) {
number=MuLu();
switch (number) {
case 1:
AddStudent(students); //添加
break;
case 2: //显示
LookStudents(students);
break;
case 3: //查询学生信息
FindStu(students);
break;
case 4: //更改学生信息
ChangeStu(students);
break;
case 5:
Paiming(students); //排名
break;
case 6:
ShanChu(students);
break ;
case 7: //退出
break A;
}
}
IO.out(students); //退出前保存学生信息
}
public static void ShanChu(ArrayList<student> students){
System.out.println("请输入要删除学生姓名");
Scanner sc=new Scanner(System.in);
String name=sc.next();
for(int i=0;i<students.size();i++){
if(name.equals(students.get(i).getName())){
students.remove(i);
System.out.println("删除成功");
return;
}
}
System.out.println("查无此人");
}
public static int MuLu(){ //调用菜单,返回选择的功能
int number;
System.out.println("欢迎来到学生成绩管理系统");
System.out.println("1.录入学生和成绩");
System.out.println("2.显示所有学生成绩");
System.out.println("3.查询单个学生成绩");
System.out.println("4.更改学生信息");
System.out.println("5.按照总分排序");
System.out.println("6.删除学生");
System.out.println("7.退出程序");
System.out.println("请输入需要的编号");
Scanner scanner=new Scanner(System.in);
number=scanner.nextInt();
return number;
}
public static void AddStudent(ArrayList<student> students){ //添加信息
try{
System.out.println("请依次输入学生姓名,语文,数学,英语成绩");
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
double Chinese = scanner.nextDouble();
double Math = scanner.nextDouble();
double English = scanner.nextDouble();
students.add(new student(name, Chinese, Math, English));
System.out.println("添加完毕");
}catch(Exception e){
e.printStackTrace();
}
}
public static void LookStudents(ArrayList<student>students){ //显示所有信息
Paiming(students);
int i;
student stu;
if(students.size()!=0){
System.out.println("姓名\t语文\t\t数学\t\t英语\t\t总分\t\t排名");
}
for(i=0;i<students.size();i++){
stu=students.get(i);
System.out.println(stu.getName()+"\t"+stu.getChinese()+"\t"+stu.getMath()+"\t"+stu.getEnglish()+"\t"+ stu.getZongFen()+"\t"+(i+1));
}
}
public static void FindStu(ArrayList<student> students){ //根据姓名查询信息
Scanner sc=new Scanner(System.in);
System.out.println("请输入要查询学生姓名");
String name=sc.next();
for(int i=0;i<students.size();i++){
if(name.equals(students.get(i).getName())){
System.out.println(students.get(i).getName()+"\t"+students.get(i).getChinese()+"\t"+students.get(i).getMath()+"\t"+students.get(i).getEnglish()+"\t"+ students.get(i).getZongFen());
break;
}
}
}
public static void ChangeStu(ArrayList<student> students){ //修改成绩
Scanner sc=new Scanner(System.in);
System.out.println("请输入要更改学生姓名");
String name=sc.next();
for(int i=0;i<students.size();i++){
if(name.equals(students.get(i).getName())){
System.out.println("请依次输入新的语文数学英语分数");
double Chinese= sc.nextDouble(),Math=sc.nextDouble(),English=sc.nextDouble();
students.get(i).setChinese(Chinese);
students.get(i).setMath(Math);
students.get(i).setEnglish(English);
students.get(i).Zongfen();
}
}
}
public static void Paiming(ArrayList<student>students){ //对成绩排名
int i,j;
for(i=0;i<students.size()-1;i++){
for(j=i+1;j<students.size();j++){
if(students.get(i).getZongFen()<students.get(j).getZongFen()){
student stu=students.get(i);
students.set(i,students.get(j));
students.set(j,stu);
}
}
}
}
}
//输入数据样例
/*
张二 57.0 57.0 58.0
张四 56.0 56.0 56.0
张三 55.0 55.0 55.0
*/
学生类
public class student{
private String name;
private double Chinese,Math,English,ZongFen;
public student(){
}
public student(String name,double Chinese,double Math,double English) throws Exception{
if(Chinese<0||Math<0||English<0||Chinese>150||Math>150||English>150){
Exception e=new Exception("分数录入出现错误");
throw e;
}
this.name=name;
this.Chinese=Chinese;
this.Math=Math;
this.English=English;
Zongfen();
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setChinese(double Chinese){
this.Chinese=Chinese;
}
public double getChinese(){
return Chinese;
}
public void setMath(double Math){
this.Math=Math;
}
public double getMath(){
return Math;
}
public void setEnglish(double English){
this.English=English;
}
public double getEnglish(){
return English;
}
public void Zongfen(){
ZongFen=Chinese+Math+English;
}
public double getZongFen(){
return ZongFen;
}
}
工具类(文件读写功能)
import java.util.*;
import java.io.*;
public class IO {
public static void out(ArrayList<student> stu){ //写入
File file = new File("D:\\ChengJi"); //创建对象
//判断是否存在这个文件
try {
FileOutputStream out = new FileOutputStream(file);
DataOutputStream ou = new DataOutputStream(out);
int n = stu.size();
ou.writeInt(n); //先写入学生总数量,方便读取
for (int i = 0; i < stu.size(); i++) {
String name = stu.get(i).getName();
double Chinese = stu.get(i).getChinese();
double Math = stu.get(i).getMath();
double English = stu.get(i).getEnglish();
ou.writeUTF(name);
ou.writeDouble(Chinese);
ou.writeDouble(Math);
ou.writeDouble(English);
}
out.close();
} catch (IOException e) {
}
}
public static void in(ArrayList<student> stu){
File file = new File("D:\\ChengJi");
if(file.exists()){ //存在就读取
try{
FileInputStream in=new FileInputStream(file);
DataInputStream inOver=new DataInputStream(in);
int n=inOver.readInt();
for(int i=0;i<n;i++){
String name =inOver.readUTF();
double Chinese=inOver.readDouble();
double Math=inOver.readDouble();
double English=inOver.readDouble();
try {
student st = new student(name, Chinese, Math, English);
stu.add(st);
}catch (Exception e){
}
}
}catch(IOException e){
}
}else{ //不存在就创建
try{
file.createNewFile(); //不存在时创建这个文件
}catch(Exception e) {
}
}
}
}