Java 爪哇语;找不到符号“;错误
我应该编写一个菜单驱动程序,允许用户在数组中输入动物和他们吃的食物,然后按名称搜索动物并检索它们的饮食。我不断地犯这些错误,我不知道为什么Java 爪哇语;找不到符号“;错误,java,Java,我应该编写一个菜单驱动程序,允许用户在数组中输入动物和他们吃的食物,然后按名称搜索动物并检索它们的饮食。我不断地犯这些错误,我不知道为什么 Main.java:33: error: cannot find symbol inventory myZoo=new inventory(); ^ symbol: class inventory location: class zoo Main.java:33: error: cannot find symbol
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;
班级动物园
{
字符串[][]动物;
公共动物园(){
字符串[][]动物=新字符串[5][2];
扫描仪输入=新扫描仪(System.in);
对于(int i=0;i而言,当您尝试实例化库存类时,似乎无法找到该类。请确保如果该类的名称为“是”,则使用import语句将其导入到您的程序中,如下所示:
import pakageName.className;
您的程序无法找到类资源清册
。请确保正确导入该类。您的代码有几个问题。让我们先讨论一下:
弗斯特
您没有任何名为inventory
的类。因此它应该是
zoo myZoo = new zoo();
//instead of
inventory myZoo=new inventory();
第二
input.close()
您不应该关闭系统。在
中。想知道它是如何关闭的吗?Scanner.close()
实际上关闭了底层流。请参阅。来自文档:
如果此扫描仪尚未关闭,则如果其底层readable也实现了Closeable接口,则将调用readable的close方法。如果此扫描仪已关闭,则调用此方法将无效
第三
在java世界中,使用大写的类名是一个很好的习惯。所以你应该使用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);
}
}
import java.util.Scanner;
班级动物园{
私有字符串[][]动物;
公共动物园(){
动物=新字符串[5][2];
扫描仪输入=新扫描仪(System.in);
对于(int i=0;i<5;i++){
System.out.println(“\n输入动物:”);
动物[i][0]=input.nextLine();
对于(int j=1;j<2;j++){
System.out.println(“\n输入动物吃的食物:”);
动物[i][j]=input.nextLine();
}
}
System.out.println(“所有动物输入!!!”;
}
公共动物{
System.out.println(“这是动物园里所有的动物…”);
对于(int i=0;i<5;i++){
System.out.println(动物[i][0]+“吃”+动物[i][1]);
}
}
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
字符串用户响应;
int myLoop=1;
Zoo myZoo=null;
while(myLoop==1){
println(“这个程序将允许你在数据库中输入5只动物和它们吃的1种食物。”);
println(“此外,你还可以按名称搜索动物,并找出它们吃什么食物。”);
System.out.println(“如果您想输入5只动物和它们吃的食物,请输入'a'”);
System.out.println(“如果要搜索动物,请输入2”);
System.out.println(“如果要退出程序,请输入'Q'\n”);
userResponse=input.nextLine();
userResponse=userResponse.toUpperCase();
if(userResponse.equals(“Q”)){
myLoop=0;
}
if(userResponse.equals(“A”)){
myZoo=新动物园();
}
if(userResponse.equals(“2”)){
如果(myZoo==null){
System.out.println(“首先输入动物信息…\n\n”);
}否则{
System.out.println(“输入要搜索的动物名称:”);
字符串animalToSearch=input.nextLine();
myZoo.findAnimal(动物研究);
}
}
}
}
public void findAnimal(字符串animalName){
String matchResult=“未找到动物”+animalName+”;
字符串animalToMatch=animalName.toUpperCase();
动物食品;
对于(int i=0;i<5;i++){
if(动物[i][0].toUpperCase().equals(动物匹配)){
动物食品=动物[i][1];
matchResult=“该动物”+animalName+”的饮食包含“+animalFood+”\n\n”;
}
}
系统输出打印项次(匹配结果);
}
}
注意:在使用之前,请仔细查看整个代码。例如,有一些微妙的修改,包括类名!我不明白你所说的importimport pakageName.className是什么意思;我应该把它放在哪里?这似乎是在运行,但它要求我在提示m之前输入信息enu,也是在我在中输入内容之后,我在线程“main java.util.NoSuchElementException:在zpp的java.util.Scanner.nextLine中找不到行”中得到了这个错误异常。main@metzmn211您的文件名仍然是zoo.java,但在我的代码中,类名是zoo.java(大写Z),遵循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);
}
}