简体   繁体   English

如何添加从用户输入中创建的值并将其传输到LinkedList?

[英]How can I add the value I created from user input and transfer that to a LinkedList?

Below is the following code I have created. 以下是我创建的以下代码。 My question is how can I create a method that returns a value created from user input and then place that value at the end of my LinkedList? 我的问题是如何创建一个返回从用户输入创建的值的方法,然后将该值放在我的LinkedList的末尾? For example the computer will prompt me to enter the char 'a' to add another client. 例如,计算机将提示我输入字符“ a”以添加另一个客户端。 How can I create a method that stores that added client information in the LinkedList? 如何创建将添加的客户信息存储在LinkedList中的方法?

 import java.io.*;
 import java.util.*;

    public class Clients implements Serializable
    {   
        private LinkedList<Person> clients = new LinkedList<Person>();
        private int id = 1;

        private static Scanner in = new Scanner(System.in);

        public static String nextLine()
        {   
            return in.nextLine(); 
        }

        public static char nextChar()
        {   
            return in.nextLine().charAt(0); 
        }

        public Clients()
        {   
            clients.add(new Person("Homer", id++));
            clients.add(new Person("Marge", id++));   
        }

        public void addClients()
        {
            //??????????
            //????????????
        }
}

假设我没有丢失任何东西,也没有验证输入,我想那应该是这样的:

clients.add(new Person(nextLine(), id++);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我将如何创建LinkedList,然后根据输入创建值。 然后,如果输入与LinkedList中的值匹配,则返回该值? - How would I create a LinkedList, then create a value from input. Then if input matches value in LinkedList return that value? 如何根据用户输入从java中的链表中删除特定元素? - How can I remove specific elements from a linkedlist in java based on user input? 如何从初始化 LinkedList 的不同类向 LinkedList 添加元素? - How can I add elements to a LinkedList from a different class that the LinkedList is initialized in? 如何将链表添加到特里叶? - How can I add a linkedlist to a trie leaf? 如何将输入从 XML 传输到 Java? - How can I transfer an input from XML to Java? 我可以使方法从LinkedList中的值中忽略字符吗? - Can I make a method ignore a character from a value in a LinkedList? 如何将LinkedList addObject添加到游戏中 - How can I add LinkedList addObject to my game 如何将 integer 值从一项活动转移到另一项活动? - How can I transfer integer value from one activity to another? 如何将JTextField的输入转换为double。 输入值来自用户 - How can I convert input from JTextField into double. Input value is from user 如何从 void 方法(Java)更改 LinkedList - How can I change a LinkedList from a void method (Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM