Cannot resolve symbol

huangapple go评论81阅读模式
英文:

Cannot resolve symbol

问题

以下是翻译好的代码部分:

package Zoo;

import java.util.Scanner;

public class Animals {

    // 实例字段
    String Name;
    String Animal;
    String Color;
    int AgeYear;
    String FavoriteFood;

    // 构造方法
    public Animals(String name, String animal, String color, int ageYear, String favoriteFood) {
        Name = name;
        Animal = animal;
        Color = color;
        AgeYear = ageYear;
        FavoriteFood = favoriteFood;
    }

    // 主方法
    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("请输入动物的名称,以查看相关信息。");
        Scanner input = new Scanner(System.in);
        String userInput = input.nextLine();

        if (userInput.equals(dolphin.Animal)) {
            System.out.println(
                dolphin.Animal + " " + dolphin.Name + " " + dolphin.AgeYear + " " + dolphin.Color + " " + dolphin.FavoriteFood);
        }
    }
}

请注意,我对代码进行了一些修正,包括将输入与 dolphin.Animal 进行比较时,我使用了 equals 方法。此外,我添加了一个字符串变量 userInput 来保存用户输入的内容,然后将其与 dolphin.Animal 进行比较。

英文:

So I'm currently just messing around with classes and objects to fully understand how it works, and tried to create a tiny search engine that would give information based on user input. The user basically has to enter the name of an animal (so far only 2 animals added).

I tried creating an if statement, so that if the input == animal (for instance a dolphin) it would spit out information on the dolphin. It won't recognize the "animal" in dolphin.animal, and I cannot seem to understand the issue.

Any help would be appreciated!
Here's my code:

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);

        }


    }
}

EDIT: yes I'm aware that the outprint is unfinished, but that I can fix afterwards of course

答案1

得分: 0

if (input == dolphin.Animal)

你还需要将input.nextLine()的结果赋值给一个变量,然后将该变量与dolphin.Animal进行比较。

英文:
if(input == dolphin.animal) 

should be

if (input == dolphin.Animal)

also you have to assign the result of input.nextLine() to a variable and then compare that variable with dolphin.Animal

答案2

得分: 0

javadoc 是你的朋友。Scanner 类中的方法 nextLine() 返回一个 String。你需要将该值保存在一个变量中,例如使用以下代码:

String theName = input.nextLine();

然后你需要使用方法 equals() 来判断 theName 是否等于 "dolphin",即:

if (theName.equals("dolphin")) {
   // 打印细节
}

顺便说一下,如果你遵循 Java 命名约定,可以让其他人更容易阅读和理解你的代码。

英文:

javadoc is your friend. Method nextLine() in class Scanner returns a String. You need to save that value in a variable, for example using this code:

String theName = input.nextLine();

Then you need to use method equals() to see if theName equals "dolphin", i.e.

if (theName.equals(dolphin.Animal)) {
   // print the details
}

By the way, if you adhere to the java naming conventions you make it easier for others to read and understand your code.

huangapple
  • 本文由 发表于 2020年10月4日 03:17:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64188086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定