java - 重复用户输入,同时将其添加到数组列表

标签 java arrays arraylist

我试图询问用户是否想将另一个人添加到数组列表“list”中。我的想法是循环询问,然后将这些输入添加到数组“inf”中,然后将该数组放入数组列表中。现在,当我重复代码时,它只会删除以前的输入;我该如何制作它以便在每次迭代后添加到数组列表中?

public class test {

    public static void main(String[] args) throws FileNotFoundException {

        Scanner scan = new Scanner(System.in);

        String ask = "no";

        do {
            System.out.println("Add? (yes/no)");
            ask = scan.nextLine();
            if (ask.equals("yes")){

                //gather user info
                System.out.println("Last Name: ");
                String LastN = scan.nextLine();


                System.out.println("First Name: ");
                String FirstN = scan.nextLine();


                System.out.println("# of Children:");
                String Children = scan.nextLine();


                System.out.println("Address:");
                String Adr = scan.nextLine();

                System.out.println("Phone #:");
                String Pho = scan.nextLine();

                Date dategather = new Date();   
                String Date = String.format("%tD", dategather);

                //Input the data into an array
                String[] inf = {LastN,FirstN,Children,Adr,Date,Pho};

                ArrayList<String> list = new ArrayList<String>();

                Collections.addAll(list,inf);

                System.out.println(list);               

            }

            } while (ask.equals("yes"));

        }
    }

最佳答案

您希望在 do-while 结构之外声明列表,并且如果您想打印每个行条目 - 那么您可以在 do- 的末尾执行此操作while结构:

 //here you declare the list OUTSIDE:

 ArrayList<String> list = new ArrayList<String>();

  do {
            System.out.println("Add? (yes/no)");
            ask = scan.nextLine();
            if (ask.equals("yes")){

                //gather user info
                System.out.println("Last Name: ");
                String LastN = scan.nextLine();

                System.out.println("First Name: ");
                String FirstN = scan.nextLine();

                System.out.println("# of Children:");
                String Children = scan.nextLine();

                System.out.println("Address:");
                String Adr = scan.nextLine();

                System.out.println("Phone #:");
                String Pho = scan.nextLine();

                Date dategather = new Date();   
                String Date = String.format("%tD", dategather);

                //Input the data into an array
                String[] inf = {LastN,FirstN,Children,Adr,Date,Pho};

                Collections.addAll(list,inf);
                //so here you want to display what the person just entered:
                System.out.println("You Have Entered : \n Last Name: "+LastN+"  First Name: "+FirstN+" Children : "+Children+" Address: "+Adr+" Date of Birth: "+Date+" Phone Number: "+Pho);               

            }

            } while (ask.equals("yes"));

希望这对您有帮助。请尝试一下并告诉我。

关于java - 重复用户输入,同时将其添加到数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37687429/

相关文章:

java - Python 替代 Java 小程序?

java - 将绘图添加到面板

java - 在java中将double解包为字符串

java - 在选择 Collection 类型方面需要帮助

java - 使用 firestore 在 for 循环中设置 boolean 值仅适用于列表中的一个对象

java - Spring Tomcat : Non-whitespace characters are not allowed in schema elements . 锯 '301 Moved Permanently'

javascript - 无论输入如何,函数都返回 true

javascript - jQuery 连接数组数据

java - 马尔可夫链,基于概率的随机文本。 java

java - 优化java代码以更少的时间对数组/数组列表进行排序