存储userinput的java arraylist

时间:2010-12-16 12:33:20

标签: java arrays arraylist user-input

当用户键入yes时,如何让它返回启动另一个目录,以及如何使用打印格式打印arraylist

感谢

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 scanner = new Scanner(System.in); 


    {
        System.out.println("Please enter your name: ");
        name.add(scanner.next());
        System.out.println("Please enter your number: ");
        phone.add(scanner.nextInt());

        System.out.println("Do you want to add a directory yes/no?");
        String answer = scanner.nextLine(); 

        if (answer.equals("yes"));
        //want it to go back to start another direcotry here

        else 
    {

        System.out.println("Thanks for adding to the directory");
        System.out.println(answer());
    }


    }

    }
}

4 个答案:

答案 0 :(得分:1)

尝试使用while,然后,当用户输入no时,您会破坏外观。代码看起来像这样:

ArrayList<String> name = new ArrayList<String>();
ArrayList<Integer> phone = new ArrayList<Integer>(); 
Scanner scanner = new Scanner(System.in); 

while(true){
    System.out.println("Please enter your name: ");
    name.add(scanner.next());
    System.out.println("Please enter your number: ");
    phone.add(scanner.nextInt());

    System.out.println("Do you want to add a directory yes/no?");
    String answer = scanner.next(); 

    if (answer.equals("no")){
        System.out.println("Thanks for adding to the directory");
        System.out.println(answer());
        break; //
    }

}

还有其他策略,例如使用do / while。但基本上你必须使用循环。

答案 1 :(得分:1)

您可以轻松打印出ArrayList:

System.out.println(list);

它将打印如下内容:

[一,二,三]

其中一,二,三是调用toString()或数组列表元素的结果。

您可以通过以下方式轻松删除[]

System.out.println(list.toString().replace("[", "").replace("]", ""));

您需要使用自定义格式工具打印循环:

for (String s : list) {
    System.out.println(format(s));
}

答案 2 :(得分:0)

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 scanner = new Scanner(System.in);
        String answer = "";

        {
            do {
                System.out.println("Please enter your name: ");
                name.add(scanner.next());
                System.out.println("Please enter your number: ");
                phone.add(scanner.nextInt());
                System.out.println("Do you want to add a directory yes/no?");
                answer = scanner.next();
            } while (answer.equals("yes"));
            if (answer.equals("yes")); //want it to go back to start another direcotry here
            else {

                System.out.println("Thanks for adding to the directory");
                for (int i = 0; i < name.size(); i++) {
                    System.out.print(name.get(i)+"\t");
                    System.out.print(phone.get(i));
                    System.out.println("");
                }
            }


        }

    }
}

答案 3 :(得分:0)

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

public class COL {
    public static void main(String [] args) {


        List<Integer> phone = new ArrayList<Integer>(); 
        Scanner scanner =new Scanner (System.in);

        while (true) {
            System.out.println("Please enter your number: ");
            phone.add(scanner.nextInt());

            System.out.println("Do you want to add a directory yes/no?");
            String answer = scanner.next(); 
            if (answer.equals("no")){
                System.out.println("Thanks for adding to the ArrayList");
                System.out.println("\t "+phone);
                break; //
            }

        }
    }

}