我试图让用户在我的virtualZoo.java文件运行时选择一种动物。它可以编译,但是一旦用户输入选择,我就会收到一条错误消息,显示“错误树类型”。下面的代码用于virtualZoo.java,animal.java和dog.java。有人指示我使用switch语句下面的对象,但不了解其实现。
virtualZoo.java
import java.util.Scanner;
public class VirtualZoo{
public static void main(String[] args) {
Animal cat = new Animal("Cat", "Meow");
Animal dog = new Animal("Dog", "Woof");
Animal duck = new Animal("Duck", "Quak");
// create Scanner
Scanner input;
input = new Scanner(System.in);
double userInput;
System.out.println("Welcome to the Zoo");
System.out.println("Pick select an animal to visit");
System.out.println("=================================");
System.out.println("===========MAIN MENU=============");
System.out.println("=================================");
System.out.println("== 0) Cat ===================");
System.out.println("== 1) Dog ===================");
System.out.println("== 2) Duck ===================");
System.out.println("== -1) EXIT ===================");
System.out.println("=================================");
System.out.println();System.out.println();
System.out.println( "Input : ");
Scanner sc = new Scanner(System.in);
userInput = sc.nextInt();
switch (sc.nextInt()) {
case 0:
System.out.println(cat);
break;
case 1:
System.out.println(dog);
break;
case 2:
System.out.println(duck);
break;
case -1:
System.out.println("Your name is short length.");
break;
default:
break;
}
duck.speak();
dog.speak();
cat.speak();
}
}
animal.java(virtualZoo.java中的动物类)
public class Animal {
private String animalSound;
private String animalType = "";
//set animal sound
public void setSound(String sound) { this.animalSound = sound; }
//get animal sound
public String getSound() { return animalSound; }
public void setType(String type) { this.animalType = type; }
//get animal type
public String getType() { return animalType; }
public Animal(String animalType, String animalSound)
{
this.animalSound = "";
this.animalType = animalType;
this.animalSound = animalSound;
}
public void speak()
{
System.out.println("The " + animalType + " says " + animalSound);
}
}
dog.java(动物类中的狗类)
public class Dog extends Animal {
public Dog(String animalType, String animalSound) {
super(animalType, animalSound);
}
@Override
public void speak(){
System.out.println("This dog barks");
}
}
答案 0 :(得分:0)
您步入正轨,但我相信您有一些错误。我假设您发布的代码与为您提供错误的代码略有不同(我已测试,但未收到错误)
在这里,您将从用户那里获得2个输入。我相信您只想得到1。
userInput = sc.nextInt();
switch (sc.nextInt())
此外,userInput
应该是int
类型,而不是双精度。
您有狗班,但没有猫/鸭班
您似乎正在尝试使用工厂模式,但我不确定100%。您可以创建一个AnimalFactory
类。否则,“动物”类会令人困惑。我假设您没有使用工厂模式。
您会注意到一些事情:
我从Object覆盖了toString()
方法。 System.out.println
将调用toString()
方法。
猫,鸭和狗都从Animal
延伸,因此您的Animal animalSelected
可以是任何一种!
动物有2种抽象方法。这些方法必须在任何子类(猫,狗,鸭)中被覆盖。声明方法意味着我们可以在Animal类中使用这些方法。它还允许我们在任何Animals上调用方法。
例如,即使Animal没有定义该方法的主体,您也可以使用animalSelected.getAnimalSound()
。
import java.util.Scanner;
public class VirtualZoo
{
public static void main(String[] args)
{
// Options
final int catType = 0,
dogType = 1,
duckType = 2,
exit = -1;
// create Scanner
Scanner input;
input = new Scanner(System.in);
int userInput;
System.out.println("Welcome to the Zoo");
System.out.println("Pick select an animal to visit");
System.out.println("=================================");
System.out.println("===========MAIN MENU=============");
System.out.println("=================================");
System.out.println("== " + catType + ") Cat ===================");
System.out.println("== " + dogType + ") Dog ===================");
System.out.println("== " + duckType + ") Duck ===================");
System.out.println("== " + exit + ") EXIT ===================");
System.out.println("=================================");
System.out.println();
System.out.println();
System.out.println( "Input : ");
Scanner sc = new Scanner(System.in);
userInput = sc.nextInt();
Animal animalSelected = null;
switch (userInput)
{
case catType:
animalSelected = new Cat();
break;
case dogType:
animalSelected = new Dog();
break;
case 2:
animalSelected = new Duck();
break;
case -1:
System.out.println("Your name is short length.");
break;
default:
break;
}
if (animalSelected != null)
{
System.out.println(animalSelected);
}
}
}
public abstract class Animal
{
public abstract String getAnimalSound();
public abstract String getAnimalType();
@Override
public String toString()
{
return "The " + getAnimalType() + " says " + getAnimalSound();
}
}
public class Duck extends Animal
{
@Override
public String getAnimalSound()
{
return "quacks";
}
@Override
public String getAnimalType()
{
return "Duck";
}
}
public class Cat extends Animal
{
@Override
public String getAnimalSound()
{
return "meows";
}
@Override
public String getAnimalType()
{
return "Cat";
}
}
public class Dog extends Animal
{
@Override
public String getAnimalSound()
{
return "barks";
}
@Override
public String getAnimalType()
{
return "Dog";
}
}