这就是现在的样子。不是整个版本,而是我的问题所在。
我希望启动时在程序中有几条狗被添加到寄存器/数组列表中。
boolean toEnd = false;
Scanner keyboard = new Scanner(System.in);
ArrayList<Dog> dogRegister = new ArrayList<Dog>();
while (toEnd == false){
System.out.println("\nWhat would you like to do? \n Press 1 to register
a dog. \n Press 2 to get a look at the taillengths of the dogs. \n Press
3 to delete a dog from the register.\n Press 4 to quit.");
int command = keyboard.nextInt(); //alternatives stored in "command"
switch (command){ //Execute chosen command in switch-statement
case 1: //User registers a dog
Dog d1 = new Dog();
Dog d2 = new Dog("Mira", "Miniature Schnauzer", 1, 8);
Dog d3 = new Dog("Jack", "Jack Russell", 3, 6);
Dog d4 = new Dog("Charlie", "Pug", 5, 5);
Dog d5 = new Dog("Max", "Dachshund", 9, 5);
Dog d6 = new Dog("Bingo", "Golden Retriever", 5, 12);
}
}
狗类
class Dog {
private String name;
private String race;
private int fage;
private double fweight;
private double taillength;
public Dog() { //Constructor
this.name = name;
this.race = race;
this.fage = fage;
this.fweight = fweight;
this.taillength = taillength;
}
}
最佳答案
只需添加,
Dog(){
}
into Dog class.
通常,编译器会提供构造函数,但只有在这种情况下,
不要定义任何一个,在这里您定义了一个参数化
构造函数,因此编译器不会提供任何默认构造函数,因此
要么添加我建议顶部的默认构造函数,要么总是
具有适当参数的新对象。