[英]java arraylist that stores userinput
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());
}
}
}
}
Try to use a while true, and then, when the user types no, you break the look.尝试使用 while true,然后,当用户键入 no 时,您会破坏外观。 The code seems like this:
代码看起来是这样的:
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.还有另一种策略,比如使用 do/while。 But essentially you have to use a loop.
但本质上你必须使用循环。
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));
}
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("");
}
}
}
}
}
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; //
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.