Closed. This question needs debugging details。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        2年前关闭。
                    
                
        

我正在尝试获得与此类似的输出-


  约翰·史密斯(John Smith)已添加到学生列表
  
  汤姆·威尔(Tom Will)已添加到学生列表
  
  您的姓名已添加到学生列表中
  
   - 开始 -
  
  姓名:约翰·史密斯(John Smith)电子邮件:[email protected]年份:2008
  
  姓名:汤姆·威尔(Tim Will)电子邮件:[email protected]年份:2007
  
  姓名:您的姓名电子邮件:您的电子邮件年份:您的年份
  
   - 结束 -
  
  汤姆·威尔(Tom Will)已从学生名单中删除
  
   - 开始 -
  
  姓名:约翰·史密斯(John Smith)电子邮件:[email protected]年份:2008
  
  姓名:您的姓名电子邮件:您的电子邮件年份:您的年份
  
   - 结束 -


但是它把它弄糊涂了。我的代码哪里出问题了?

代表学生的课程:

public class Student {

    private String name;
    private String email;
    private int year; //year of registration to the course

    /**
     *  Constructor
     *
     *@param  name, email and year of registration
     */
    public Student(String name, String email, int year){
        this.name = name;
        this.email = email;
        this.year = year;
    }

    /**
     *  get the name
     *
     *@return the name
     */
    public String getName(){
        return name;
    }


    /**
     *  A toString() method to give a String representation of a Student
     *
     *@return    The String representation of a Student
     */
    public String toString(){
        return "Name:" + name +" Email:" + email + " Year:" + year;
    }

}

public class StudentList {

    private ArrayList<Student> list; //instance variable

    /**
     *  Constructor
     */
    public StudentList(){
        list = new ArrayList<Student>();
    }

    /**
     *  a method to print off all ArrayList elements
     */

    public void printList(){
        System.out.println("--Begin--");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
            }
        System.out.println("--End--");

    }

    /**
     *  A method to add a student to the list
     *
     *@param    The student
     */
    public void addToList(Student s){
        list.add(s);

        System.out.println("--Begin--");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i) +"has been addded to the list");
            }
        System.out.println("--End--");

    }

    /**
     *  A method to remove a student from the list
     *
     *@param    The student
     */
    public void removeFromList(Student s){
        list.remove(s);
        System.out.println("--Begin--");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i) +"has been removed from the list");
        }
        System.out.println("--End--");

    }

    /**
     *  A main method to test
     */
    public static void main(String[] args) {

        // Create an instance of the class
        StudentList studentList = new StudentList();

        //create 3 student objects
        Student s1 = new Student("John Smith", "[email protected]", 2008);
        Student s2 = new Student("Tom Will", "[email protected]", 2007);
        Student s3 =  new Student("Cameron Young","[email protected]",2018);

        //add the three students to the list
        studentList.addToList(s1);
        studentList.addToList(s2);
        studentList.addToList(s3);

        // Print the list
        studentList.printList();

        // Remove the student "Tom Will"
        studentList.removeFromList(s2);

        // Print the list again
        studentList.printList();
    }

}

最佳答案

每次添加或删除用户时,您都将打印整个列表。我认为这就是您“混帐”的意思。

您的addToList方法应如下所示:

public void addToList(Student s) {
    list.add(s);
    System.out.println(s.getName() + " has been addded to the list");
}

10-04 19:50