帮忙看下导入导出 和排序有问题 没有 本人新手也看不出啥问题,就运行不出来 ,学生类我放在后面的。

package xuejiguanli;
import jxl.*;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import javax.swing.GroupLayout.Alignment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
//-------------------------------------------------------------------这是成绩管理系统的主界面
public class xueshengdei{
public static void main(String[] args) {
List Students = new ArrayList<>();

    while(true) {

   //----------------------------------------------------------------------菜单


        System.out.println("-----------------学生管理系统----------------");
        System.out.println("-------欢迎使用重庆工程学院学籍管理系统-------");
        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(" 8-------------导入学生信息");
        System.out.println(" 9-------------导出学生信息");
        System.out.println(" 10------------退出该系统");
        Scanner sc =new Scanner(System.in);
        System.out.println("请输入序号选择其对应的功能:");
        String j = sc.nextLine();
        switch (j) {
        case "1":
            //增加学生
            addstudent(Students);
            break;
        case "2":
            //删除学生
            deletestudent(Students);
            break;
        case "3":
            //修改学生
            changestudent(Students);
            break;
        case "4":
                //单独查找
             singlefind(Students);
            break;
        case "5":
            //查看所有学生
            findstudent(Students);
            break;
        case "6":
            //排序
            System.out.println("请输入你要是实现的排序功能:");
            Scanner s =new Scanner(System.in);
            System.out.println("1---------------成绩排序:");
            System.out.println("2---------------年龄排序:");
            System.out.println("3---------------学号排序:");
            String second=s.nextLine();
            switch(second) {


            case "1":   paixu(Students);break;
            case "2":   paixu3(Students); break;
            case "3":  paixu2(Students);  break;
            }
            break;
        case "7":
            tongzhi(Students);

            //查看学校通知
            break;
        case "8":

            exceldaoru(Students);

            //导入学生信息
            break;
        case "9":
            daochu(Students);
            //导出学生信息
            break;
        case "10":
            System.out.println("谢谢你的使用!");
            System.exit(0);//退出

        default:

            System.out.println("输入错误请重新输入!"); 
            break;
        }
    }
}   
//--------------------------------------------------------------导出
public static void daochu(List<Student> Students) {
String excelpath;
Scanner scanner=new Scanner(System.in);
System.out.println("操作:导出数据");
System.out.println("请输入导出Excel文件路径:");
excelpath = scanner.next();
try {
FileOutputStream fos = new FileOutputStream(excelpath);
WritableWorkbook wwb = Workbook.createWorkbook(fos);
// 创建工作表
WritableSheet ws = wwb.createSheet("学生学籍表", 10);
// 设置单元格的文字格式
WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);

ws.setColumnView(4, 10);
ws.setColumnView(5, 16);
ws.setColumnView(0, 14);
ws.addCell(new Label(0, 0, "学号", wcf));
ws.addCell(new Label(1, 0, "姓名", wcf));
ws.addCell(new Label(2, 0, "年龄", wcf));
ws.addCell(new Label(3, 0, "性别", wcf));
ws.addCell(new Label(4, 0, "成绩", wcf));
// 填充数据的内容
// 匹配学生学号
System.out.println("所有学生学籍信息如下:");
int cnt = 1;
while (cnt <= Students.size()) {

Student s = (Student) Students.get(cnt - 1);
ws.addCell(new Label(4, cnt, String.valueOf(s.getId()), wcf));
ws.addCell(new Label(1, cnt, String.valueOf(s.getName()), wcf));
ws.addCell(new Label(2, cnt, String.valueOf(s.getAge()), wcf));
ws.addCell(new Label(3, cnt, String.valueOf(s.getSex()), wcf));
ws.addCell(new Label(4, cnt, String.valueOf(s.getScore()), wcf));
cnt++;
if (cnt == 0)
wcf = new WritableCellFormat();
}
wwb.write();
wwb.close();
System.out.println("学生信息导出成功!");
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}


}

//---------------------------------------------------------------导入
public static void exceldaoru(List<Student> Students) {
    Scanner scanner=new Scanner(System.in);
        String excelpath;
        excelpath = scanner.next();
    WritableWorkbook book = null;
    try {

            // 创建一个Excel文件对象
        FileOutputStream fos = new FileOutputStream(excelpath);
            // 创建Excel第一个选项卡对象
         WritableWorkbook book1 = Workbook.createWorkbook(new File(excelpath));
            WritableSheet sheet = book1.createSheet("第一页", 0);
            // 设置表头,第一行内容
            // Label参数说明:第一个是列,第二个是行,第三个是要写入的数据值,索引值都是从0开始
            Label label1 = new Label(0, 0, "学号");// 对应为第1列第1行的数据
            Label label2 = new Label(1, 0, "姓名");// 对应为第2列第1行的数据
            Label label3 = new Label(2, 0, "年龄");// 对应为第3列第1行的数据
            Label label4 = new Label(3, 0, "性别");// 对应为第4列第1行的数据
            // 添加单元格到选项卡中
            sheet.addCell(label1);
            sheet.addCell(label2);
            sheet.addCell(label3);
            sheet.addCell(label4);
            // 遍历集合并添加数据到行,每行对应一个对象
            for (int i = 0; i < Students.size(); i++) {
                    Student s = Students.get(i);
                    // 表头占据第一行,所以下面行数是索引值+1
                    // 跟上面添加表头一样添加单元格数据,这里为了方便直接使用链式编程
                    sheet.addCell(new Label(0, i + 1, s.getId()+""));
                    sheet.addCell(new Label(1, i + 1, s.getName()));
                    sheet.addCell(new Label(2, i + 1, s.getAge()+""));
                    sheet.addCell(new Label(3, i + 1, s.getSex()));
                    sheet.addCell(new Label(4, i + 1, s.getScore()+""));
            }
            // 写入数据到目标文件
            book.write();
    } catch (Exception e) {
            e.printStackTrace();
    } finally {
            try {
                    // 关闭
                    book.close();
            } catch (Exception e) {
                    e.printStackTrace();
            }
    }

}

    //----------------------------------------------------------------------------------通知
public static void tongzhi(List<Student> Students) {
    int index = -1;

    if(Students.size()==0) {
        System.out.println("请你输入学生后再来查看通知,谢谢!");
        return;
    }

   int[] point = new int[100]; 
    for (int i = 0; i < Students.size(); i++) {
        Student s = Students.get(i);
        point[i] = s.getScore();


    }
        for (int i = 0; i < Students.size(); i++) {
            if (point[i]<60){
    Student s = Students.get(i);
                System.out.println(s.getName()+"同学,你收到的通知如下:");
        System.out.println("恭喜你挂科了");
        System.out.println("恭喜你挂科了");
        System.out.println("恭喜你挂科了");
        System.out.println("重要的事情说三遍");
            }
            else {
                Student s = Students.get(i);
                System.out.println(s.getName()+"同学,你收到的通知如下:");
                System.out.println("哎呦不错哟");
                System.out.println("恭喜你没有挂科");


            }

        } 


    }


//-----------------------------------------------------------------------------------成绩排序
public static void paixu(List<Student> Students) {
    if(Students.size()==0) {
        System.out.println("不好意思,目前还没有学生的信息可供排序,请返回重新操作!");
        return;
    }

   int[] point = new int[100]; 
    for (int i = 0; i < Students.size(); i++) {
        Student s = Students.get(i);
        point[i] = s.getScore();
        Arrays.sort(point);

    }
    for (int j = 0; j < point.length; j++) {
        for (int i = 0; i < Students.size(); i++) {
            if (point[j]==Students.get(i).getScore()){
                Student s = Students.get(i);
                System.out.println("学号\t姓名\t年龄\t性别\t成绩");   
                System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getSex()+"\t"+s.getScore());               

            }

        } 


    }
    }


//--------------------------------------------------------------------学号排序
public static void paixu2(List<Student> Students) {
    if(Students.size()==0) {
        System.out.println("不好意思,目前还没有学生的信息可供排序,请返回重新操作!");
        return;
    }

   int[] point = new int[10000000]; 
    for (int i = 0; i < Students.size(); i++) {
        Student s = Students.get(i);
        point[i] = s.getId();
        Arrays.sort(point);

    }
    for (int j = 0; j < point.length; j++) {
        for (int i = 0; i < Students.size(); i++) {
            if (point[j]==Students.get(i).getId()){
                Student s = Students.get(i);
                System.out.println("学号\t姓名\t年龄\t性别\t成绩");   
                System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getSex()+"\t"+s.getScore());               

            }

        } 


    }
    }
//---------------------------------------------------------------------------------------年龄排序
public static void paixu3(List<Student> Students) {
    if(Students.size()==0) {
        System.out.println("不好意思,目前还没有学生的信息可供排序,请返回重新操作!");
        return;
    }

   int[] point = new int[100]; 
    for (int i = 0; i < Students.size(); i++) {
        Student s = Students.get(i);
        point[i] = s.getAge();
        Arrays.sort(point);

    }
    for (int j = 0; j < point.length; j++) {
        for (int i = 0; i < Students.size(); i++) {
            if (point[j]==Students.get(i).getAge()){
                Student s = Students.get(i);
                System.out.println("学号\t姓名\t年龄\t性别\t成绩");   
                System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getSex()+"\t"+s.getScore());               

            }

        } 


    }
    }









//-------------------------------------------------------------------------------------------添加
public static void addstudent(List<Student> Students) {

    //创建键盘录入对象
    Scanner sc= new Scanner(System.in);
    while(true) {
    System.out.println("请输入学号:");
    int id= sc.nextInt();
    System.out.println("请输入姓名:");
    String name = sc.nextLine();
    System.out.println("请输入年龄:");
    int age = sc.nextInt();
    System.out.println("请输入性别:");
    String sex = sc.nextLine();
    System.out.println("请输入成绩:");
    int score = sc.nextInt();

    //创建学生对象
    Student s = new Student();
    s.setId(id);
    s.setName(name);
    s.setAge(age);
    s.setSex(sex);
    s.setScore(score);

    //把学生对象添加到集合
    Students.add(s);
    break;
    }
    System.out.println("添加学生成功!");

}
//-----------------------------------------------------------------------------------------(查找所有学生)
public static void findstudent(List<Student> Students) {

    if(Students.size()==0) {
        System.out.println("不好意思,目前还没有学生的信息可供查询,请返回重新操作!");
        return;
    }
    for(int i=0;i<Students.size();i++) {
        Student s = Students.get(i);
        System.out.println("学号:"+s.getId()+"\n姓名:"+s.getName()+"\n性别:"+s.getSex()+"\n年龄:"+s.getAge()+"\n成绩:"+s.getScore());
    }
}
//----------------------------------------------单独查找-----------------------------------------------

public static void singlefind(List students) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入要查找的学号");
int findId=sc.nextInt();
int index = -1;
for(int i=0;i<students.size();i++) {
Student s = students.get(i);
if(s.getId()==findId) {

index=i;

}

if (index == -1) {
System.out.println("你要查找的学生不存在,请重新选择!");
}else {
System.out.println("学号:"+s.getId()+"\n姓名:"+s.getName()+"\n性别:"+s.getSex()+"\n年龄:"+s.getAge()+"\n成绩:"+s.getScore());

}

}
}

//------------------------------------------------------------------------------------------------删除
public static void deletestudent(List<Student> Students) {

    Scanner sc= new Scanner(System.in);
    System.out.print("请输入要删除的学号:");
    int deleId = sc.nextInt();
    int index = -1;
    for(int i=0;i<Students.size();i++) {
        Student s = Students.get(i);
        if(s.getId()==deleId) {     
            index=i;                    
        }           
        if (index == -1) {
            System.out.println("你要删除的学生不存在,请重新选择!");
        } else {
            Students.remove(index);
            System.out.println("删除学生成功!!");
        }
    } 
}
//---------------------------------------------------------------------------------------修改
public static void changestudent(List<Student> students) {

    Scanner sc= new Scanner(System.in);
    System.out.print("请输入要改变的学号:");
    int changeId = sc.nextInt();
    int index = -1;
    for(int i=0;i<students.size();i++) {
        Student s = students.get(i);
        if(s.getId()==changeId) {       
            index = i;              
        }
    }
    if (index == -1) {
        System.out.println("您要改变的学生不存在,请重新操作!");
    } else {
        System.out.println("请输入新的姓名:");
        String name = sc.nextLine();
        System.out.println("请输入新的年龄:");
        int age = sc.nextInt();
        System.out.println("请输入新的性别:");
        String sex = sc.nextLine();
        System.out.println("请输入新的成绩:");
        int score = sc.nextInt();

        //创建学生对象
        Student s = new Student();
        s.setId(changeId);
        s.setName(name);
        s.setAge(age);
        s.setSex(sex);
        s.setScore(score);
        //修改学生对象
        students.set(index,s);

        System.out.println("修改学生成功!!!");
    }
}

}

//学生类

package xuejiguanli;
public class Student {
private String name;
private int Id;
private int age;
private String sex;
private int score;

public Student() {

}
public Student(String name, int Id, int age, String sex,int score) {
    super();
    this.name = name;
    this.Id = Id;
    this.age = age;
    this.sex = sex;
    this.score =score;

}
public String getName() {
    return name;
}//姓名
public int getId() {
    return Id;
}//学号
  public int getAge() {
    return age;
}//年龄
public String getSex() {
    return sex;
}//性别
public int getScore() {
    return score;
}//成绩




  public void setName(String name) {
    this.name = name;
}

 public void setId(int Id) {
    this.Id = Id;
}
 public void setAge(int age) {
    this.age = age;
}

 public void setSex(String sex) {
     this.sex = sex;
 }
  public void setScore(int score) {
    this.score = score;

}

}

1个回答

报什么错都不发一下?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问