我正在尝试询问用户是否要将其他人添加到数组列表中," 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"));
}
}
答案 0 :(得分:1)
您可以创建一个Person类,其中包含每个人的所有详细信息,并将它们添加到ArrayList中。像这样的东西: (提示:你可以将#children作为整数而不是String)
public class test
{
public static void main(String[] args) throws FileNotFoundException {
ArrayList<Person> list = new ArrayList<Person>();
do
{
System.out.println("Add? (yes/no)");
ask = scan.nextLine();
if (ask.equals("yes")){
System.out.println("Last Name: ");
String lastName= scan.nextLine();
System.out.println("First Name: ");
String firstName= scan.nextLine();
System.out.println("# of Children:");
String children = scan.nextLine();
System.out.println("Address:");
String address= scan.nextLine();
System.out.println("Phone #:");
String phone= scan.nextLine();
Date dategather = new Date();
String date = String.format("%tD", dategather);
list.add(new Person(lastName, firstName, children,address, phone,date));
}
}while (ask.equals("yes"));
}
}
public class Person
{
String lastName, firstName, children,address, phone,date
public Person(String lastName,String firsName,String children,String address,String phone, String date)
{
this.lastName=lastName;
this.firsName=firsName;
this.children=children;
this.address=address;
this.phone=phone;
this.date=date;
}
}
在每个循环的代码中,每次重新初始化时,arrayList都会重置。
答案 1 :(得分:1)
您希望在do-while
结构之外声明列表,如果您想打印每个行条目,那么您可以在do-while
结构的末尾执行此操作:< / p>
//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"));
我希望这会对你有所帮助。请试一试,让我知道。