Java的ArrayList中存储用户输入 [英] Java Arraylist to store user input

查看:796
本文介绍了Java的ArrayList中存储用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是新来的ArrayList和Java,我想知道,如果有人可以帮助我或者给我关于如何创建一个程序,它允许用户从键盘重复输入目录条目并将它们存储在一个ArrayList。指针

Hi I am new to arraylists and java and I was wondering if someone could help me or give me pointers on how to create a program that allows the user to repeatedly enter directory entries from the keyboard and store them in an arraylist.

enter name:
enter telephone number:

和然后,如果用户想要输入另一个问

and then ask if the user wants to enter another one

enter another:  Y/N

感谢

推荐答案

您还可以使用两个的ArrayList,或者使一类的名字和电话的属性,然后进行该类的对象之一的ArrayList

You can still use two ArrayLists, or make a class with name and phone attributes and then make one ArrayList of objects of that class.

这里显示的第一种方法。

First approach shown here.

import java.util.ArrayList;
import java.util.Scanner;

public class AAA {

    public static void main(String[] args) {
        ArrayList<String> name = new ArrayList<String>();
        ArrayList<Integer> phone = new ArrayList<Integer>();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("Please enter your name: ");
            name.add(sc.next());
            System.out.println("Please enter your number: ");
            phone.add(sc.nextInt());
        }
    }
}

这篇关于Java的ArrayList中存储用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆