这就是它现在的样子。不是整个版本,而是我的问题所在。
我希望在启动程序中有几只狗加入到注册/ arraylist中。
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;
}
}
答案 0 :(得分:1)
您必须自己定义 no-args 构造函数:
public Dog() {
// Code
}
由于您定义了重载构造函数,编译器不会为您创建默认,因此您会收到错误。
您可以使用重载的构造函数实例化Dog
,或使用默认的加上setter:
Dog dog = new Dog(name, race, fage, fweight, taillength);
// or
Dog dog = new Dog();
dog.setName(name);
dog.setRace(race);
...
答案 1 :(得分:1)
添加,
Dog(){
}
into Dog class.
通常,编译器会给出构造函数,但仅限于此时 没有定义任何一个,在这里你定义了一个参数化 构造函数,所以编译器不会给出任何默认构造函数,所以 要么添加我建议在顶部的默认构造函数,要么总是制作 具有适当参数的新对象。
答案 2 :(得分:0)
在您的班级Dog
中,您正在创建一个包含字段的构造函数,但您应该创建一个没有如下字段的构造函数:
public Dog(){....}
你可以在另一类测试中使用它,如下所示:
Dog dog = new Dog();