gpt4 book ai didi

java - 启动时程序中的条目

转载 作者:行者123 更新时间:2023-12-02 04:08:01 25 4
gpt4 key购买 nike

这就是现在的样子。不是整个版本,而是我的问题所在。

我希望程序在启动时有几只狗被添加到寄存器/数组列表中。

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.

Generally, compiler gives constructor but only in that case while you don't define any one, here you have define one parametrized constructor, so compiler won't gives any default constructor, so either add Default constructor which i suggest at top, or Always made new Object with proper Argument.

关于java - 启动时程序中的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34064420/

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