Java 无法解析符号

Java 无法解析符号,java,Java,因此,我目前只是在摆弄类和对象,以充分了解其工作原理,并试图创建一个基于用户输入提供信息的小型搜索引擎。用户基本上必须输入一个动物的名字(到目前为止只添加了两个动物) 我尝试创建一个if语句,这样如果输入==动物(例如海豚),它就会吐出关于海豚的信息。它无法识别dolphin.animal中的“动物”,我似乎无法理解这个问题 任何帮助都将不胜感激! 这是我的密码: package Zoo; import java.util.Scanner; public class Animals {

因此,我目前只是在摆弄类和对象,以充分了解其工作原理,并试图创建一个基于用户输入提供信息的小型搜索引擎。用户基本上必须输入一个动物的名字(到目前为止只添加了两个动物)

我尝试创建一个if语句,这样如果输入==动物(例如海豚),它就会吐出关于海豚的信息。它无法识别dolphin.animal中的“动物”,我似乎无法理解这个问题

任何帮助都将不胜感激! 这是我的密码:

package Zoo;

import java.util.Scanner;

public class Animals {

    //instance fields
    String Name;
    String Animal;
    String Color;
    int AgeYear;
    String FavoriteFood;


    //constructor method
    public Animals(String name, String animal, String color, int ageYear, String favoriteFood) {
        Name = name;
        Animal = animal;
        Color = color;
        AgeYear = ageYear;
        FavoriteFood = favoriteFood;
    }

    //main method
    public static void main(String[] args) {
        Animals dolphin = new Animals("Gretha", "dolphin", "gray", 2, "salmon");
        Animals orangutan = new Animals("Peter", "orangutan", "black", 15, "bananas");


        System.out.println("Please enter the name of an animal to view information regarding said animal.");
        Scanner input = new Scanner(System.in);
        input.nextLine();

        if(input == dolphin.animal)  {
            System.out.println(dolphin.Animal + dolphin.Name + dolphin.AgeYear + dolphin.Color + dolphin.FavoriteFood);

        }


    }
}
编辑:是的,我知道输出尚未完成,但我当然可以在之后修复

if(input == dolphin.animal) 
应该是

if (input == dolphin.Animal)
您还必须将input.nextLine()的结果分配给一个变量,然后将该变量与dolphin进行比较。Animal是您的朋友。类
扫描器中的方法
返回一个
字符串
。您需要将该值保存在变量中,例如使用以下代码:

String theName=input.nextLine();
然后您需要使用方法
equals()
来查看
theName
是否等于“dolphin”,即

if(名称等于海豚动物)){
//打印详细信息
}

顺便说一句,如果你遵守了规则,你会让其他人更容易阅读和理解你的代码。

你定义的字段称为
dolphin.Animal
,不是
dolphin.animal
input
是一个
扫描器
不是一个字符串。您可以阅读下一行,但不要将其分配给变量。请不要在问题中编辑“已解决”之类的内容。我已回滚该编辑。如果你成功地解决了问题,请接受帮助你解决问题的答案,或者将答案与你的解决方案一起发布,并在超时后接受。下次我会记得的,很抱歉,谢谢!这一切都成功了。:)