输出不会打印链接列表中的所有列表。使用java

输出不会打印链接列表中的所有列表。使用java,java,algorithm,data-structures,linked-list,Java,Algorithm,Data Structures,Linked List,我试着用链表列出学生注册系统。因此,您可以将名称添加到列表中,但问题是输出仅打印用户添加的第一个名称,而不是所有名称 我试着加上迈克尔、凯特和罗斯的名字。但输出仅显示Michael 输出: *****学生课程注册制度***** --------------------菜单----------------- 将新学员添加到课程中 删除/撤消注册 显示学生信息 编辑学生数据 在课程中搜索学生 出口 你的选择:3 学生姓名: 迈克尔 请帮助我显示我添加到列表中的所有名称。 先谢谢你。你的帮助对我真的很

我试着用链表列出学生注册系统。因此,您可以将名称添加到列表中,但问题是输出仅打印用户添加的第一个名称,而不是所有名称

我试着加上迈克尔、凯特和罗斯的名字。但输出仅显示Michael

输出:

*****学生课程注册制度*****

--------------------菜单-----------------

  • 将新学员添加到课程中
  • 删除/撤消注册
  • 显示学生信息
  • 编辑学生数据
  • 在课程中搜索学生
  • 出口
  • 你的选择:3

    学生姓名: 迈克尔

    请帮助我显示我添加到列表中的所有名称。 先谢谢你。你的帮助对我真的很有帮助

    
    public class LinkedList2nd {
        Node head; 
    
        static class Node 
    {   
       // Data fields for Node   
       Object info;  // data stored in the node
       Node link;         // link to next node
    
       // Methods
       // Constructors
       // postcondition: Creates a new empty node.
       public Node() {
         info = null;
         link = null;
    
       }
    
       // postcondition: Creates a new node storing obj.
       public Node(Object obj) {
         info = obj;
         link = null;
       }
    
       // postcondition: Creates a new node storing obj 
       //   and linked to node referenced by next.
       public Node(Object obj, Node next) {
         info = obj;
         link = next;
       } 
       // accessors
    
       public Object getInfo() 
       {
          return info;
       }
    
       public Node getLink() 
       {
          return link;
       }
    
    
       // mutators
       public void setInfo(Object newInfo) 
       {
          info = newInfo;
       }
    
       public void setLink(Node newLink) 
       {
          link = newLink;
       }
    } 
    
        public static LinkedList2nd insert(LinkedList2nd list,Object info){
            Node newNode = new Node(info);
            if(list.head == null){
                list.head = newNode;
            }
            else {
              //inserting last node 
                Node current = list.head;
               while(current.getLink() != null){
                current = current.getLink();
                current.setLink(newNode); }//while 
    
                }//else
    
            return list;
        }
    
        public static void printList(LinkedList2nd list){
            Node current = list.head; 
    
            System.out.println("Student Name\n--------------");
    
            while(current != null){
              System.out.println(current.getInfo() + " ");
              current = current.link;//to next node 
    
            }//while
    
            System.out.println("\n");
        }
    
    }
    
    
    
    //`enter code here`MAIN CLASS : 
    
    
    public class Main {
    //STUDENT COURSE REGISTRATION SYSTEM 
        static LinkedList2nd name = new LinkedList2nd();
        //static LinkedList matric = new LinkedList();
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
            int choice;
            do {
                menu();
                System.out.print("Your choice:");
                choice=sc.nextInt();
                if (choice==0){
                  System.out.println("Thank you and Good Bye.");
                break;
                  }
                else 
                processChoice(choice);
                } while (choice !=0);
              }//end main
    
    
    
        static void menu(){
            System.out.println("*****STUDENT COURSE REGISTRATION SYSTEM*****");
            System.out.println("********************************************");
            System.out.println("-------------------- MENU ------------------\n"
                            +"1. ADD NEW STUDENT TO COURSE\n"
                            +"2. REMOVE/WITHDRAW ENROLLMENT\n"
                            +"3. DISPLAY STUDENT INFORMATION\n"
                            +"4. EDIT STUDENT DATA\n"
                            +"5. SEARCH STUDENT IN THE COURSE\n"
                            +"0. EXIT");
              }//menu
    
        static void processChoice(int choice){
            switch (choice) {
                case 1:
                    addStudent();
                    break;
    
                case 3:
                    displayList();
                    break; 
    
                default:
                    break;
            }    
        }
    
        static void addStudent(){
            Scanner read = new Scanner(System.in);
            System.out.println("Enter Student Name : ");
            String inputName = read.nextLine();
            name.insert(name, inputName);
    
    
        }
    
        static void displayList(){
            name.printList(name);
    
        }
    
        }//class 
    

    在当前的insert方法中,您正在设置所有节点到newNode的链接。您应该将set调用移出循环

           while(current.getLink() != null){
            current = current.getLink();
            current.setLink(newNode); }//while 
    
            }//else
    
    纠正

           while(current.getLink() != null){
            current = current.getLink();
    
            }//else
            current.setLink(newNode);
    

    进行一些调试的绝佳机会。