我应该编写一个菜单驱动的程序,允许用户将动物和他们吃的食物输入一个阵列,然后按名称搜索动物并检索他们的饮食。我一直在收到这些错误,但我不知道为什么。
Main.java:33: error: cannot find symbol
inventory myZoo=new inventory();
^
symbol: class inventory
location: class zoo
Main.java:33: error: cannot find symbol
inventory myZoo=new inventory();
^
symbol: class inventory
location: class zoo
2 errors
代码:
import java.util.Scanner;
class zoo
{
String[][] animals;
public zoo() {
String[][] animals=new String[5][2];
Scanner input = new Scanner(System.in);
for (int i=0;i<5;i++) {
System.out.println("\nEnter an animal: ");
animals[i][0]=input.nextLine();
for (int j=1;j<2;j++) {
System.out.println("\nEnter a food the animal eats: ");
animals[i][j]=input.nextLine();
}
}
input.close();
return;
}
public static void main(String[] args) {
inventory myZoo=new inventory();
String[][] animals=new String[5][2];
Scanner input = new Scanner(System.in);
int myLoop=1;
String userResponse="";
while(myLoop==1) {
System.out.println("This program will allow you to enter 5 animals into a database along with 1 food they eat.");
System.out.println("\nAlso, you will be allowed to search for an animal by name and find out what food they eat.");
System.out.println("\n\n If you would like to enter 5 animals and a food they eat please enter 'A'");
System.out.println("\nIf you would like to search for an animal please enter the animals name");
System.out.println("\nIf you would like to exit the program please enter 'Q'\n");
userResponse=input.nextLine();
userResponse=userResponse.toUpperCase();
if(userResponse.equals("Q")){
myLoop=0;
}
if(userResponse.equals("A")){
myZoo();
}
if(userResponse.equals("2")){
myZoo.findAnimal(userResponse);
}
}
}
public void findAnimal(String animalName) {
//setting a return value if no match is found
String matchResult="The animal "+animalName+" was not found";
//variable declaration for animal and making uppercase
String animalToMatch=animalName.toUpperCase();
//place holder variable for animal
String arrayAnimal="";
//place holder for food
String animalFood="";
//loop to check all animals in arrayAnimal
for(int i=0;i<5;i++) {
arrayAnimal=animals[i][0];
arrayAnimal=arrayAnimal.toUpperCase();
if(arrayAnimal.equals(animalToMatch)){
animalFood=animals[i][1];
matchResult="The animal "+animalName+" has a diet that consists of "+animalFood;
}
}
System.out.println(matchResult);
}
}
答案 0 :(得分:0)
在您尝试实例化它时,似乎无法找到Inventory类。确保你的名字是否具有该类,然后使用import语句将其导入程序:
import pakageName.className;
答案 1 :(得分:0)
您的程序无法找到课程inventory
。确保正确导入课程。
答案 2 :(得分:0)
您的代码有几个问题。让我们先讨论一下:
您没有任何名为inventory
的班级。所以应该是
zoo myZoo = new zoo();
//instead of
inventory myZoo=new inventory();
input.close();
你不应该关闭System.in
。想知道如何关闭? Scanner.close()
实际上关闭了基础流。请参阅docs here。来自docs:
如果此扫描程序尚未关闭,那么如果其底层可读也实现了Closeable接口,则将调用可读的close方法。如果此扫描仪已关闭,则调用此方法将无效。
java世界中强大的convention使用大写的类名。因此,您应该使用Zoo
而不是zoo
。并不是说它会影响你的代码,但它是一种约定,当你用Java编码时,你应该遵守Java的约定,对吗?
现在,下面是可以做到的众多可能性之一。
import java.util.Scanner;
class Zoo {
private String[][] animals;
public Zoo() {
animals = new String[5][2];
Scanner input = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
System.out.println("\nEnter an animal: ");
animals[i][0] = input.nextLine();
for (int j = 1; j < 2; j++) {
System.out.println("\nEnter a food the animal eats: ");
animals[i][j] = input.nextLine();
}
}
System.out.println("All animals entered!!!");
}
public void zooAnimals() {
System.out.println("Here's all the animals in the zoo...");
for (int i = 0; i < 5; i++) {
System.out.println(animals[i][0] + " eats " + animals[i][1]);
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String userResponse;
int myLoop = 1;
Zoo myZoo = null;
while (myLoop == 1) {
System.out.println("This program will allow you to enter 5 animals into a database along with 1 food they eat.");
System.out.println("Also, you will be allowed to search for an animal by name and find out what food they eat.");
System.out.println("If you would like to enter 5 animals and a food they eat please enter 'A'");
System.out.println("Ifyou would like to search for an animal please enter 2");
System.out.println("If you would like to exit the program please enter 'Q'\n");
userResponse = input.nextLine();
userResponse = userResponse.toUpperCase();
if (userResponse.equals("Q")) {
myLoop = 0;
}
if (userResponse.equals("A")) {
myZoo = new Zoo();
}
if (userResponse.equals("2")) {
if (myZoo == null) {
System.out.println("Enter the animals information first...\n\n");
} else {
System.out.println("Enter the animal name to search:");
String animalToSearch = input.nextLine();
myZoo.findAnimal(animalToSearch);
}
}
}
}
public void findAnimal(String animalName) {
String matchResult = "The animal " + animalName + " was not found";
String animalToMatch = animalName.toUpperCase();
String animalFood;
for (int i = 0; i < 5; i++) {
if (animals[i][0].toUpperCase().equals(animalToMatch)) {
animalFood = animals[i][1];
matchResult = "The animal " + animalName + " has a diet that consists of " + animalFood + "\n\n";
}
}
System.out.println(matchResult);
}
}
注意:在使用之前,请仔细查看整个代码。有一些微妙的修改,包括例如类名!