Java 为什么我的数据不保存在数组中
你能告诉我为什么不起作用吗?我什么时候加动物? 我以前工作过,将它添加到我在菜单中创建的ArrayList中。但是我想分离BLL和UI 期待收到你们的来信:)Java 为什么我的数据不保存在数组中,java,Java,你能告诉我为什么不起作用吗?我什么时候加动物? 我以前工作过,将它添加到我在菜单中创建的ArrayList中。但是我想分离BLL和UI 期待收到你们的来信:) 公共类动物 { 私有字符串名称;//动物名称 私有字符串kindofamal;//动物种类 private int currentAge;//动物的当前年龄 private int expectedAge;//预期寿命 私有列表动物=新的ArrayList(); 公共动物(字符串名称、字符串类动物、int currentAge、int e
公共类动物
{
私有字符串名称;//动物名称
私有字符串kindofamal;//动物种类
private int currentAge;//动物的当前年龄
private int expectedAge;//预期寿命
私有列表动物=新的ArrayList();
公共动物(字符串名称、字符串类动物、int currentAge、int expectedAge)
{
this.name=名称;
this.kindofamal=kindofamal;
this.currentAge=currentAge;
this.expectedAge=expectedAge;
this.anives=新的ArrayList();
}
public int getExpectedAge()
{
返回期望值;
}
公共无效设置expectedAge(int expectedAge)
{
this.expectedAge=expectedAge;
}
公共字符串getName()
{
返回名称;
}
公共void集合名(字符串名)
{
this.name=名称;
}
公共字符串getkindofamal()
{
动物的返回种类;
}
public void setkindofamal(字符串kindofamal)
{
this.kindofamal=kindofamal;
}
public int getCurrentAge()
{
回流电流;
}
公共无效设置currentAge(int currentAge)
{
this.currentAge=currentAge;
}
公共字符串toString()
{
return“Type:“+getKindOfAnimal()+”\n”
+“名称:“+getName()+”\n”
+“年龄:“+getCurrentAge()+”\n”
+“生存期:”+getExpectedAge();
}
公共动物学家(动物)
{
动物。添加(动物);
}
公共列表获取动物()
{
归还动物;
}
}
菜单类
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();
}
}
public菜单()
{
菜单();
}
公共无效菜单()
{
扫描仪sc=新的扫描仪(System.in);
System.out.println(“请键入一个数字并按enter键”);
System.out.println(“1.添加动物”);
System.out.println(“2.展示动物”);
if(sc.hasnetint())
{
int input=sc.nextInt();
如果(输入>=0&&input<10)
{
如果(输入=1)
{
添加动物();
}
else if(输入=2)
{
展示动物();
}
}
其他的
{
System.out.println(“请输入介于范围之间的数字”);
菜单();
}
}
其他的
{
System.out.println(“输入错误,请输入一个数字!”);
菜单();
System.out.println();
}
}
私人动物
{
System.out.println(“添加动物”);
System.out.println();
扫描仪sc=新的扫描仪(System.in);
尝试
{
System.out.println(“名称:”);
字符串名称=sc.nextLine();
System.out.println(“动物种类”);
字符串kindofaimal=sc.nextLine();
System.out.println(“当前年龄:”);
int currentAge=sc.nextInt();
System.out.println(“预期年龄:”);
int expectedAge=sc.nextInt();
动物=新动物(名称、动物种类、当前年龄、预期年龄);
//添加(animal.toString());
动物学家(动物);
}
捕获(例外e)
{
System.out.println(“请输入正确的信息”);
}
System.out.println();
菜单();
}
公众动物()
{
动物=新动物(空,空,1,1);
if(animal.getAnimals().isEmpty())
{
System.out.println(“还没有动物!”;
菜单();
}
其他的
{
for(int i=0;i
在Animal
类的每个实例中都有一个单独的动物列表,因此每个列表都只有一个动物
将
动物
的列表保留在不同的类中(可能是动物园
类?)更有意义。您的问题在于showAnimals()中的这一行:
问题是,每当您想要显示动物列表时,您都要使用该行代码创建一个新列表。所以,它总是空出来
尝试将动物列表从动物
类移动到菜单
类中。这样,您将只有列表的一个副本,并且可以在其中添加和显示动物
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();
}
}
公共类菜单{
私有列表动物=新的ArrayList();
公共菜单()
{
菜单();
}
等
私人动物
{
System.out.println(“添加动物”);
System.out.println();
扫描仪sc=新的扫描仪(System.in);
尝试
{
System.out.println(“名称:”);
字符串名称=sc.nextLine();
System.out.println(“动物种类”);
字符串kindofaimal=sc.nextLine();
System.out.println(“当前年龄:”);
int currentAge=sc.nextInt();
System.out.println(“预期年龄:”);
int expectedAge=sc.nextInt();
动物=新动物(名称、动物种类、当前年龄、预期年龄);
动物。添加(动物);
}
捕获(例外e)
{
System.out.println(“请输入正确的信息”);
}
System.out.println();
菜单();
}
公众动物()
{
if(animals.isEmpty())
{
System.out.println(“还没有动物!”;
菜单();
}
其他的
{
对于(int i=0;iAnimal animal = new Animal(null, null, 1, 1);
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();
}
}