package group7.list;import java.util.*;public class Student {

private String sID ;
private String sName ;
private String sGender ;
private int sAge ;

Scanner cin = new Scanner(System.in);

public Student(){};

public Student(String sID, String sName,String gender, int age ){
this.setsName(sName);
this.setsGender(gender);
this.setsAge(age);
}

public void setsID(String sID) {
this.sID = sID;
} public String getsID() {
return sID;
} public void setsName(String sName) {
this.sName = sName;
} public String getsName() {
return sName;
} public void setsGender(String sGender) {
this.sGender = sGender;
} public String getsGender() {
return sGender;
} public void setsAge(int sAge) {
this.sAge = sAge;
} public int getsAge() {
return sAge;
}

public void addStudent(){

Student s1=new Student();
System.out.println("请输入学生学号:");
String ID=cin.nextLine();
s1.setsID(ID);
System.out.println("请输入学生姓名:");
String name=cin.nextLine();
s1.setsName(name);
System.out.println("请输入学生性别:");
String gender=cin.nextLine();
s1.setsGender(gender);
System.out.println("请输入学生年龄:");
int age=cin.nextInt();
s1.setsAge(age);

StudentList s=new StudentList();
s.addStudent(s1);
}

public void deleteStudent(){

System.out.println("请输入需要删除的学生的学号:");
String deleteID=cin.nextLine();
int index=Integer.parseInt(deleteID);

StudentList s=new StudentList();
s.removeStudent(index);

}}
package group7.list;import java.util.ArrayList;
import java.util.Iterator;public class StudentList {

ArrayList<Student> stuList=new ArrayList<Student>();

public StudentList(){}

public StudentList(Student student){

stuList.add(student);
}

public void addStudent( Student student){

stuList.add(student);

};

public void removeStudent(int index){

stuList.remove(index);

};

public void showStudents(){

Iterator<Student> it= stuList.iterator();

while(it.hasNext()){
System.out.println(it.next()+" ");
}

};}
package group7.list;import java.util.*;public class StudentListTest { /**
 * 2、 使用List管理对象集合
         1) 新建一个包group_x.list(x表示所在的组号)
         2) 在这个包中新建三个类:Student类,StudentList类,StudentListTest类。
            Student类主要包括:
 
         3) 完善上面三个类,相关要求参考源代码程序的注释,即根据要求修改源代码程序,给出具体的实现代码。
     *@author 
 * @param args
 */

public static void main(String[] args) {


Scanner cin=new Scanner(System.in);


Student stu0 = new Student("001","Jack","M",20);
Student stu1 = new Student("002","Mike","M",20);
Student stu2 = new Student("003","Lisa","F",20);
Student stu3 = new Student("004","Linda","F",20);
Student stu4 = new Student("005","Harry","M",20);
Student stu5 = new Student("006","Jimmy","M",20);

StudentList s10=new StudentList(stu0);
StudentList s11=new StudentList(stu1);
StudentList s12=new StudentList(stu2);
StudentList s13=new StudentList(stu3);
StudentList s14=new StudentList(stu4);
StudentList s15=new StudentList(stu5);

StudentListTest s= new StudentListTest();
s.menu();

int choice=cin.nextInt();

switch(choice){
case 1 : {

boolean flag=true;

while(flag)
{
Student s1=new Student();
    s1.addStudent();
    System.out.println("如果想要继续添加请输入y,否则输入n");
    String c=cin.next();
    if(c=="y") flag=true;
    else flag=false;
    
}

StudentListTest s5=new StudentListTest();
s5.menu();
};

break;

case 2 :{

boolean flag=true;

while(flag){
Student s1=new Student();
s1.deleteStudent();
System.out.println("如果想要继续删除请输入y,否则输入n");
    String c=cin.next();
    if(c=="y") flag=true;
    else flag=false;
}

StudentListTest s6=new StudentListTest();
s6.menu();

};

break;

case 3 :{

StudentList s2=new StudentList();
s2.showStudents();

StudentListTest s7=new StudentListTest();
s7.menu();

};

break;

case 4 :{
StudentListTest s8=new StudentListTest();
s8.menu();
};
break;
}
} private void menu() {

System.out.println("<<<请选择你需要的操作>>>");
System.out.println("|   1、添加学生信息        |");
System.out.println("|   2、删除学生信息        |");
System.out.println("|   3、显示学生信息        |");
System.out.println("|   4、退出                            |");
System.out.println("************************");

}}
怎样改进这个程序 使得这个程序能够正常运行?为什么使用功能2的时候会抛出异常?为什么使用功能3的时候会什么都不显示?

解决方案 »

  1.   


    public class StudentListTest { /**
     * 2、 使用List管理对象集合 1) 新建一个包group_x.list(x表示所在的组号) 2)
     * 在这个包中新建三个类:Student类,StudentList类,StudentListTest类。 Student类主要包括:
     * 
     * 3) 完善上面三个类,相关要求参考源代码程序的注释,即根据要求修改源代码程序,给出具体的实现代码。
     * 
     * @author
     * @param args
     */ public static void main(String[] args) { Scanner cin = new Scanner(System.in); Student stu0 = new Student("001", "Jack", "M", 20);
    Student stu1 = new Student("002", "Mike", "M", 20);
    Student stu2 = new Student("003", "Lisa", "F", 20);
    Student stu3 = new Student("004", "Linda", "F", 20);
    Student stu4 = new Student("005", "Harry", "M", 20);
    Student stu5 = new Student("006", "Jimmy", "M", 20); StudentList s10 = new StudentList(stu0);
    StudentList s11 = new StudentList(stu1);
    StudentList s12 = new StudentList(stu2);
    StudentList s13 = new StudentList(stu3);
    StudentList s14 = new StudentList(stu4);
    StudentList s15 = new StudentList(stu5); // StudentListTest s = new StudentListTest();
    menu(); int choice = cin.nextInt(); switch (choice) {
    case 1: { boolean flag = true; while (flag) {
    Student s1 = new Student();
    s1.addStudent();
    System.out.println("如果想要继续添加请输入y,否则输入n");
    String c = cin.next();
    if (c != "y")
    flag = true;

    }
    menu();
    // StudentListTest s5 = new StudentListTest();

    }
    ; break; case 2: { boolean flag = true; while (flag) {
    Student s1 = new Student();
    s1.deleteStudent();
    System.out.println("如果想要继续删除请输入y,否则输入n");
    String c = cin.next();
    if (c != "y")
    flag = true;
    } // StudentListTest s6 = new StudentListTest();
    //menu(); }
    ; break; case 3: { StudentList s2 = new StudentList();
    s2.showStudents(); // StudentListTest s7 = new StudentListTest();
    menu(); }
    ; break; case 4: {
    // StudentListTest s8 = new StudentListTest();
    menu();
    }
    ;
    break;
    }
    } private static void menu() { System.out.println("<<<请选择你需要的操作>>>");
    System.out.println("|   1、添加学生信息        |");
    System.out.println("|   2、删除学生信息        |");
    System.out.println("|   3、显示学生信息        |");
    System.out.println("|   4、退出                            |");
    System.out.println("************************"); }}
      

  2.   

    第二个menu()不要注释掉,忘了解注释了~
    楼主的Java代码写出了C的风格,呵呵~