根据迭代次数将字符串用户输入添加到数组列表中



我正在做一个Java项目,我有我的超级类和私有属性。我使用从 Main 中的超类继承的访问器和突变器来设置和获取这些属性。我正在创建一个 for 循环,以根据用户的 INT 编号遍历输入。我被困在如何获取 Name 的用户输入并将其添加到循环中的 ArrayList 中,所以当它完成时,我应该有 X 个名称。

Scanner  input = new Scanner(System.in);
ArrayList<String> animalList = new ArrayList<String>();
//local variables
char     choice;
int      choice2;
String   mInputN;
String   mInputT;
String   mInputG;
int      mInputA;
double   mInputW;
String   mInputD;
//user input if else statement for Y or N
System.out.println("Would you like to add an animal? Y or N ");
choice = input.next().toUpperCase().charAt(0);
if (choice == 'Y')  {
System.out.println("How many animals would you like to add?");
choice2 = input.nextInt();
for (i = 0; i < choice2; ++i)  {
System.out.println("Enter new animal name: ");
mInputN = input.next();
System.out.println("Enter type of animal: ");
mInputT = input.next();
System.out.println("Enter gender of animal: ");
mInputG = input.next();
System.out.println("Enter age of animal: ");
mInputA = input.nextInt();
System.out.println("Enter weight of animal: ");
mInputW = input.nextDouble();
System.out.println("Date acquired (DD/MM/YYYY");
mInputD = input.next();
RescueAnimal animal[i] = new RescueAnimal();
animal[i].setName(mInputN);
animal[i].setType(mInputT);
animal[i].setGender(mInputG);
animal[i].setAge(mInputA);
animal[i].setWeight(mInputW);
animal[i].setAcquisitionDate(mInputD);
animalList.add(mInputN);
System.out.println(animal[i].getName() + ", Age: " + animal[i].getAge() + ", Gender: " + animal[i].getGender());
System.out.print("Type: " + animal[i].getType() + ", ");
System.out.print("Weight: " + animal[i].getWeight() + ", ");
System.out.println("Acquisition Date: " + animal[i].getAcquisitionDate());
System.out.println();
System.out.println(animalList);
}

好吧,如果你只是要做System.out.println(animalList);那么你只会得到一个内存位置,而不是这个列表中的精算内容; 所以我个人做的是在用户输入并完成循环后,在第一个 for 循环中编写此代码。对于每个循环来说,这是一个:(删除最后一个System.out.println();(

for(String i : animalList){
System.out.println(i);
}

或者你也可以做:

for(int i = 0; i < animalList.length; i++){
System.out.println(animalList[i]);
}

而且在你打印完动物的信息后,做:

System.out.println("Acquisition Date: " + animal[i].getAcquisitionDate() + "n);

而不是:

System.out.println("Acquisition Date: " + animal[i].getAcquisitionDate());
System.out.println();

首先是代码。代码后的解释。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class RescueAnimal {
private String name;
private String type;
private String gender;
private int age;
private double weight;
private String acquisitionDate;
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
public double getWeight() {
return weight;
}
public String getAcquisitionDate() {
return acquisitionDate;
}
public void setName(String name) {
this.name = name;
}
public void setType(String type) {
this.type = type;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setAge(int age) {
this.age = age;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void setAcquisitionDate(String acquisitionDate) {
this.acquisitionDate = acquisitionDate;
}
public static void main(String[] args) {
List<RescueAnimal> animalList = new ArrayList<>();
Scanner input = new Scanner(System.in);
System.out.println("Would you like to add an animal? Y or N ");
char choice = input.nextLine().toUpperCase().charAt(0);
if (choice == 'Y')  {
System.out.println("How many animals would you like to add?");
int choice2 = input.nextInt();
input.nextLine();
for (int i = 0; i < choice2; ++i)  {
System.out.println("Enter new animal name: ");
String mInputN = input.nextLine();
System.out.println("Enter type of animal: ");
String mInputT = input.nextLine();
System.out.println("Enter gender of animal: ");
String mInputG = input.nextLine();
System.out.println("Enter age of animal: ");
int mInputA = input.nextInt();
input.nextLine();
System.out.println("Enter weight of animal: ");
double mInputW = input.nextDouble();
input.nextLine();
System.out.println("Date acquired (DD/MM/YYYY");
String mInputD = input.nextLine();
RescueAnimal animal = new RescueAnimal();
animal.setName(mInputN);
animal.setType(mInputT);
animal.setGender(mInputG);
animal.setAge(mInputA);
animal.setWeight(mInputW);
animal.setAcquisitionDate(mInputD);
animalList.add(animal);
System.out.println(animal.getName() + ", Age: " + animal.getAge() + ", Gender: " + animal.getGender());
System.out.print("Type: " + animal.getType() + ", ");
System.out.print("Weight: " + animal.getWeight() + ", ");
System.out.println("Acquisition Date: " + animal.getAcquisitionDate());
System.out.println();
System.out.println(animalList.size());
}
}
}
}

您希望创建RescueAnimal列表,而不是String列表。
您尚未声明任何数组,因此不需要[i]ArrayList不是数组。
您要做的只是根据用户输入的详细信息创建RescueAnimal实例,然后将该RescueAnimal实例添加到列表中。

相关内容

  • 没有找到相关文章

最新更新