写了一个小的控制台的信息系统,但是其中有问题,看了好久,不知道什么原因,请大神看看
程序代码:
package Homework; import java.util.*; public class Work9 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); try{ System.out.println("请输入学生的总人数:"); int size = sc.nextInt(); Manage ma = new Manage(size); ma.addStu(); //问题从这里开始:跳到下面 ma.getGrade(); ma.getStuNumber(); ma.count(); ma.removeStu(); }catch(Exception e){ e.printStackTrace(); }finally{ try{ sc.close(); }catch(Exception e){ e.printStackTrace(); } } } } class Manage { ArrayList<Student> al = null; public Manage(int size) { al = new ArrayList<Student>(size); } public void addStu() { Scanner sc = new Scanner(System.in); try{ System.out.println("cuowu isnot here"); for(int i=0; i<al.size(); i++) //for循环就没有被执行这是为什么呢? { System.out.println("cuowu isnot here"); System.out.print("请输入第"+(i+1)+"个学生的学号:"); //另外想问的就是,若创建了多个Scanner输入流,怎么关闭,当关了一个就会抛出异常的,为什么啊? String number = sc.nextLine(); System.out.print("请输入第"+(i+1)+"个学生的姓名:"); String name = sc.nextLine(); System.out.print("请输入第"+(i+1)+"个学生的成绩:"); float grade = sc.nextFloat(); System.out.println(); Student st = new Student(); st.setName(name); st.setNumber(number); st.setGrade(grade); al.add(st); } }catch(Exception e){ e.printStackTrace(); } System.out.println(); } public void getGrade() { Scanner sc = new Scanner(System.in); try{ System.out.println("请输入该学生的学号: "); String number = sc.nextLine(); for(int i=0; i<al.size(); i++) { Student st = al.get(i); if(st.getNumber().equals(number)) { System.out.println("该学生的成绩为:"+st.getGrade()); } } }catch(Exception e){ e.printStackTrace(); } System.out.println(); } public void getStuNumber() { Scanner sc = new Scanner(System.in); try{ System.out.println("请输入该学生的成绩: "); float grade = sc.nextFloat(); for(int i=0; i<al.size(); i++) { Student st = al.get(i); if(st.getGrade()==grade) { System.out.println("得到该成绩的学生学号为:"+st.getNumber()); } } }catch(Exception e){ e.printStackTrace(); } System.out.println(); } public void count() { int A = 0; int B = 0; int C = 0; int D = 0; for(int i=0; i<al.size(); i++) { Student st = al.get(i); float grade = st.getGrade(); if(grade>=90) { A++; } else if(grade>=80 && grade<90) { B++; } else if(grade>=70 && grade<80) { C++; } else { D++; } } System.out.println("得到优的学生共有== "+A+" ==人"); System.out.println("得到良的学生共有== "+B+" ==人"); System.out.println("得到中的学生共有== "+C+" ==人"); System.out.println("得到差的学生共有== "+D+" ==人"); System.out.println(); } public void removeStu() { Scanner sc = new Scanner(System.in); try{ System.out.println("请输入想要删除的学生的学号"); String number = sc.nextLine(); for(int i=0; i<al.size(); i++) { Student st = al.get(i); if(st.getNumber() == number) { al.remove(i); break; } } }catch(Exception e){ e.printStackTrace(); }finally{ try{ sc.close(); }catch(Exception e){ e.printStackTrace(); } } } } class Student { private String number; private String name; private float grade; public float getGrade() { return grade; } public void setGrade(float grade) { this.grade = grade; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } }