gpt4 book ai didi

java - 为什么我的数据没有保存在数组中

转载 作者:行者123 更新时间:2023-11-30 03:09:47 25 4
gpt4 key购买 nike

当我添加动物时,你能告诉我为什么它不起作用吗?我以前工作过,当我将它添加到我在菜单中创建的 ArrayList 时。但我想将 BLL 和 UI 分开。

期待收到你们的来信:)

public class Animal
{

private String name; //Name of animal
private String kindOfAnimal; //Kind of animal
private int currentAge; //Current age of animal
private int expectedAge; //Expected lifetime

private List<Animal> animals = new ArrayList<>();

public Animal(String name, String kindOfAnimal, int currentAge, int expectedAge)
{
this.name = name;
this.kindOfAnimal = kindOfAnimal;
this.currentAge = currentAge;
this.expectedAge = expectedAge;
this.animals = new ArrayList<>();
}

public int getExpectedAge()
{
return expectedAge;
}

public void setExpectedAge(int expectedAge)
{
this.expectedAge = expectedAge;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public String getKindOfAnimal()
{
return kindOfAnimal;
}

public void setKindOfAnimal(String kindOfAnimal)
{
this.kindOfAnimal = kindOfAnimal;
}

public int getCurrentAge()
{
return currentAge;
}

public void setCurrentAge(int currentAge)
{
this.currentAge = currentAge;
}

public String toString()
{
return "Type: " + getKindOfAnimal() + '\n'
+ "Name: " + getName() + '\n'
+ "Age: " + getCurrentAge() + '\n'
+ "Lifetime: " + getExpectedAge();
}

public void setAnimalList(Animal animal)
{
animals.add(animal);
}

public List<Animal> getAnimals()
{
return animals;
}
}

菜单类

   public Menu()
{
menu();
}

public void menu()
{
Scanner sc = new Scanner(System.in);

System.out.println("Please type in a number and press enter");
System.out.println("1. Add animal");
System.out.println("2. Show animals");

if (sc.hasNextInt())
{
int input = sc.nextInt();

if (input >= 0 && input < 10)
{
if (input == 1)
{
addAnimal();
}
else if (input == 2)
{
showAnimals();
}

}
else
{
System.out.println("Please enter a number between the range");
menu();
}
}
else
{
System.out.println("Wrong input, please input a number!");
menu();
System.out.println();
}

}

private void addAnimal()
{
System.out.println("ADD ANIMAL");
System.out.println();

Scanner sc = new Scanner(System.in);

try
{
System.out.println("Name: ");
String name = sc.nextLine();

System.out.println("Kind of animal: ");
String kindOfAnimal = sc.nextLine();


System.out.println("Current age: ");
int currentAge = sc.nextInt();

System.out.println("Expected age: ");
int expectedAge = sc.nextInt();

Animal animal = new Animal(name, kindOfAnimal, currentAge, expectedAge);

// animals.add(animal.toString());

animal.setAnimalList(animal);
}

catch (Exception e)
{
System.out.println("Please enter the correct information");
}
System.out.println();
menu();
}

public void showAnimals()

{

Animal animal = new Animal(null, null, 1, 1);

if (animal.getAnimals().isEmpty())
{
System.out.println("There is no animals yet!");
menu();
}
else
{
for (int i = 0; i < animal.getAnimals().size(); i++)
{
System.out.println(animal.getAnimals());

}
menu();
}
}

最佳答案

您的问题出在 showAnimals() 内的这一行:

Animal animal = new Animal(null, null, 1, 1);

问题是,每次您想要显示动物列表时,您都会使用该行代码创建一个新列表。所以,它总是空的。

尝试将动物列表从 Animal 类移至 Menu 类。这样,您将只有一份列表副本,并且可以在其中添加和显示动物。

public class Menu {

private List<Animal> animals = new ArrayList<>();

public Menu()
{
menu();
}


etc...


private void addAnimal()
{
System.out.println("ADD ANIMAL");
System.out.println();

Scanner sc = new Scanner(System.in);

try
{
System.out.println("Name: ");
String name = sc.nextLine();

System.out.println("Kind of animal: ");
String kindOfAnimal = sc.nextLine();


System.out.println("Current age: ");
int currentAge = sc.nextInt();

System.out.println("Expected age: ");
int expectedAge = sc.nextInt();

Animal animal = new Animal(name, kindOfAnimal, currentAge, expectedAge);

animals.add(animal);
}

catch (Exception e)
{
System.out.println("Please enter the correct information");
}

System.out.println();
menu();
}

public void showAnimals()
{
if (animals.isEmpty())
{
System.out.println("There is no animals yet!");
menu();
}
else
{
for (int i = 0; i < animals.size(); i++)
{
System.out.println(animals.get(i));
}
menu();
}
}

关于java - 为什么我的数据没有保存在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33854924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com