存储用户输入的java arraylist
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4460788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java arraylist that stores userinput
提问by George
how can I get it to go back to start another directory when the user types yes and also how do I print the arraylist out using a print format
当用户输入 yes 以及如何使用打印格式打印数组列表时,如何让它返回以启动另一个目录
thanks
谢谢
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());
}
}
}
}
回答by pmariano
Try to use a while true, and then, when the user types no, you break the look. The code seems like this:
尝试使用 while true,然后,当用户键入 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; //
}
}
There are another strategies, like using a do/while. But essentially you have to use a loop.
还有另一种策略,比如使用 do/while。但本质上你必须使用循环。
回答by AlexR
You can easily print out an ArrayList:
您可以轻松打印出一个 ArrayList:
System.out.println(list);
It will print something like:
它将打印如下内容:
[one, two, three]
[一二三]
where one, two, three are the results of invocation of toString() or array list's elements.
其中一、二、三是调用 toString() 或数组列表元素的结果。
You can remove [] easily by:
您可以通过以下方式轻松删除 []:
System.out.println(list.toString().replace("[", "").replace("]", ""));
You you need to use your custom format implement print in loop:
您需要使用自定义格式实现循环打印:
for (String s : list) {
System.out.println(format(s));
}
回答by jasmin
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("");
}
}
}
}
}
回答by harjinder rai
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; //
}
}
}
}