通过使用arraylist添加元素来创建接口

时间:2017-08-10 12:11:44

标签: java

我在使用arraylist在我的集合中添加一组元素时遇到了问题。我有一个有3个变量的班级学生。添加,删除和获取学生的界面,但在我的测试中,我无法显示不同的结果。

String studId = "";
String firstName = "";
String lastName = "";
int number = 0;
int index = 0;
StudentServiceImpl studServImpl = new StudentServiceImpl();
Scanner scan = new Scanner(System.in);
Student student = new Student();
    do {
        System.out.println("1.Add Student:");
        System.out.println("2.Delete Student:");
        System.out.println("3.Display Students:");
        System.out.println("4.Exit");
        System.out.println();
        System.out.print("Select number: ");
        System.out.println();
        number = scan.nextInt();
        if (number == 1) {
            System.out.println("Enter Student No.");
            studId = scan.next();
            student.setStudId(studId);
            System.out.println("Enter First Name:");
            firstName = scan.next();
            student.setFirstName(firstName);
            System.out.println("Enter Last Name:");
            lastName = scan.next();
            student.setLastName(lastName);
            studServImpl.add(student);
        } else if (number == 2) {
            // TODO:
        } else if (number == 3) {
            List<Student> studentList = new ArrayList<Student>();
            studentList = studServImpl.getStudent();
            System.out.println("\nThe size of List: " + studentList.size());
            for (Student studentRec : studentList) {
                System.out.println("Student ID: " + studentRec.getStudId());
                System.out.println("First Name: " + studentRec.getFirstName());
                System.out.println("Last Name: " + studentRec.getFirstName() + "\n");
            }
        } else if (number == 4) {
            System.out.println("Thank you, Good Bye!");
        }
    } while (number != 0);

它的输出是

输入1表示添加

我输入要求的值

然后重复它,我的列表中的大小为2

但即使输入{123,Jun,Jack},{135,Jon,Bill}:

等不同的值,它也会显示相同的输出
results:

Student ID: 123
FirstName: Jun
LastName: Jack

Student ID: 123
FirstName: Jun
LastName: Jack

HELP !!

2 个答案:

答案 0 :(得分:0)

您在循环外部实例化student,并使用菜单项1多次添加。

应该在循环中移动实例化...

答案 1 :(得分:0)

String studId = "";
String firstName ="";
String lastName ="";
int number =0;
int index = 0;
StudentServiceImpl studServImpl = new StudentServiceImpl();
Scanner scan = new Scanner(System.in);

do{

System.out.println("1.Add Student:");
System.out.println("2.Delete Student:");
System.out.println("3.Display Students:");
System.out.println("4.Exit");
System.out.println();
System.out.print("Select number: ");
System.out.println();
number=scan.nextInt();

if(number==1){
    Student student =  new Student();
    System.out.println("Enter Student No.");
    studId = scan.next();
    student.setStudId(studId);

    System.out.println("Enter First Name:");
    firstName = scan.next();
    student.setFirstName(firstName);

    System.out.println("Enter Last Name:");
    lastName = scan.next();
    student.setLastName(lastName);

    studServImpl.add(student);

}


    else if(number ==2){



    }

    else if (number == 3){

    List<Student> studentList = new ArrayList<Student>();

    studentList = studServImpl.getStudent();

    System.out.println("\nThe size of List: "+studentList.size());



    for (Student studentRec : studentList) {
        System.out.println("Student ID: "+ studentRec.getStudId());
        System.out.println("First Name: "+ studentRec.getFirstName());
        System.out.println("Last Name: "+ studentRec.getLastName()+"\n");


    }

    }

    else if(number == 4){

        System.out.println("Thank you, Good Bye!");
    }



    }


    while(number!=0);

}

您的代码可能就是这样,请注意我所做的唯一更改是使学生对象成为您进行输入的块内的局部变量。