我试图让用户在运行时从我的 virtualZoo.java 文件中选择一个动物。它可以编译,但是一旦用户输入了一个选择,我就会收到一个错误,上面写着“错误的树类型”。下面是 virtualZoo.java、animal.java 和 dog.java 的代码。我在 switch 语句下创建了对象,因为我被指示使用这些对象,但不了解实现。
虚拟动物园
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();
}
}
相关分类