宠物可以是猫还是狗。每只宠物都需要有一个名字,一个主人的名字,颜色,医生的名字和品种。所有的宠物都可以哭泣,吃饭和睡觉。 不使用切换方法,我尝试使用扫描仪的setter和getter功能。但我不知道如何确定用户是否输入狗然后用户将输入所有关于狗的信息,然后输入猫。
这可能吗?
package petexercise;
import java.util.Scanner;
public class PetCatDog {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
PetCat cat = new PetCat();
PetDog dog = new PetDog();
System.out.print("Enter Dog or Cat Word:");
String pet = cat.nextLine();
}
}
其他子类是为setter和getters
制作的答案 0 :(得分:4)
我想如果您不想使用开关,可以使用地图。第一步用简单的字符串识别用户输入的内容,第二步创建您的宠物。
Map<String, PetFactory> factories = new HashMap<>();
factories.put("dog", new DogFactory())
...
String petType = scanner.nextLine();
factories.get(petType).createPet("name", "color");
答案 1 :(得分:2)
所以你有 -
User - 表示用户的类。有姓名,宠物信息等。用户可以有很多或没有宠物
宠物 - 代表某种动物的宠物。有一些共同的属性,如姓名,年龄......一些常见的行为,如吃。
狗 - 延伸宠物有一些属性,如名字,年龄,颜色,品种,一些特别针对狗狗的行为,提取球
猫 - 延长宠物有一些属性,如姓名,年龄,颜色,品种,一些行为,如每天睡20次无用
要求用户 -
<强>更新强>
要知道用户是输入了狗还是猫,您可以使用 -
Scanner scan = new Scanner(System.in);
String response;
do{
System.out.print("Do you have a pet ? (Y/N): ");
response = scan.nextLine();
} while(!response.equalsIgnoreCase("Y") && !response.equalsIgnoreCase("N"));
if(response.equalsIgnoreCase("N")){
System.exit(0);
}
do{
System.out.print("Cat or a dog ? (C/D): ");
response = scan.nextLine();
} while(!response.equalsIgnoreCase("C") && !response.equalsIgnoreCase("D"));
答案 2 :(得分:1)
提到的属性不是动物特定的,所以可以有一个Pet类,其中包含名称,颜色,品种和动物类型等属性:
enum Animal {
CAT(4),
DOG(4);
public final int legs;
private Animal(int legs) {
this.legs = legs;
}
}
public static Animal what(String whatAnimal) {
return Animal.valueOf(whatAnimal.toUpperCase());
};
Animal animal = what("dog");
答案 3 :(得分:1)
我明白了!感谢大家的帮助
package dogandcat;
import java.util.Scanner;
公共类CatDogSystem {
public static void main(String[] args) {
String animal;
Scanner scan = new Scanner(System.in);
Cat cat = new Cat();
Dog dog = new Dog();
System.out.print("Enter Dog or Cat Only: ");
animal = scan.nextLine();
if(animal.equalsIgnoreCase("Cat")) {
System.out.print("Enter cat's name: ");
cat.setCatName(scan.nextLine());
System.out.print("Enter owner's name: ");
cat.setCatOwnersName(scan.nextLine());
System.out.print("Enter cat's color: ");
cat.setCatColor(scan.nextLine());
System.out.print("Enter doctor's name: ");
cat.setCatDoctorsName(scan.nextLine());
System.out.print("Enter cat's breed: ");
cat.setCatBreed(scan.nextLine());
System.out.println("");
System.out.println("Cat's Details");
System.out.println(cat.getCatName());
System.out.println(cat.getCatOwnersName());
System.out.println(cat.getCatColor());
System.out.println(cat.getCatDoctorsName());
System.out.println(cat.getCatBreed());
} else if(animal.equalsIgnoreCase("Dog")) {
System.out.print("Enter Dog's name: ");
dog.setDogName(scan.nextLine());
System.out.print("Enter owner's name: ");
dog.setDogOwnersName(scan.nextLine());
System.out.print("Enter Dog's color: ");
dog.setDogColor(scan.nextLine());
System.out.print("Enter doctor's name: ");
dog.setDogDoctorsName(scan.nextLine());
System.out.print("Enter Dog's breed: ");
dog.setDogBreed(scan.nextLine());
System.out.println("");
System.out.println("Dog's Details");
System.out.println(dog.getDogName());
System.out.println(dog.getDogOwnersName());
System.out.println(dog.getDogColor());
System.out.println(dog.getDogDoctorsName());
System.out.println(dog.getDogBreed());
} else System.out.println("Invalid Input !");
}
}