Cannot resolve symbol

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

Cannot resolve symbol

问题

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

  1. package Zoo;
  2. import java.util.Scanner;
  3. public class Animals {
  4. // 实例字段
  5. String Name;
  6. String Animal;
  7. String Color;
  8. int AgeYear;
  9. String FavoriteFood;
  10. // 构造方法
  11. public Animals(String name, String animal, String color, int ageYear, String favoriteFood) {
  12. Name = name;
  13. Animal = animal;
  14. Color = color;
  15. AgeYear = ageYear;
  16. FavoriteFood = favoriteFood;
  17. }
  18. // 主方法
  19. public static void main(String[] args) {
  20. Animals dolphin = new Animals("Gretha", "dolphin", "gray", 2, "salmon");
  21. Animals orangutan = new Animals("Peter", "orangutan", "black", 15, "bananas");
  22. System.out.println("请输入动物的名称,以查看相关信息。");
  23. Scanner input = new Scanner(System.in);
  24. String userInput = input.nextLine();
  25. if (userInput.equals(dolphin.Animal)) {
  26. System.out.println(
  27. dolphin.Animal + " " + dolphin.Name + " " + dolphin.AgeYear + " " + dolphin.Color + " " + dolphin.FavoriteFood);
  28. }
  29. }
  30. }

请注意,我对代码进行了一些修正,包括将输入与 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:

  1. package Zoo;
  2. import java.util.Scanner;
  3. public class Animals {
  4. //instance fields
  5. String Name;
  6. String Animal;
  7. String Color;
  8. int AgeYear;
  9. String FavoriteFood;
  10. //constructor method
  11. public Animals(String name, String animal, String color, int ageYear, String favoriteFood) {
  12. Name = name;
  13. Animal = animal;
  14. Color = color;
  15. AgeYear = ageYear;
  16. FavoriteFood = favoriteFood;
  17. }
  18. //main method
  19. public static void main(String[] args) {
  20. Animals dolphin = new Animals("Gretha", "dolphin", "gray", 2, "salmon");
  21. Animals orangutan = new Animals("Peter", "orangutan", "black", 15, "bananas");
  22. System.out.println("Please enter the name of an animal to view information regarding said animal.");
  23. Scanner input = new Scanner(System.in);
  24. input.nextLine();
  25. if(input == dolphin.animal) {
  26. System.out.println(dolphin.Animal + dolphin.Name + dolphin.AgeYear + dolphin.Color + dolphin.FavoriteFood);
  27. }
  28. }
  29. }

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

答案1

得分: 0

  1. if (input == dolphin.Animal)

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

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

should be

  1. 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。你需要将该值保存在一个变量中,例如使用以下代码:

  1. String theName = input.nextLine();

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

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

顺便说一下,如果你遵循 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:

  1. String theName = input.nextLine();

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

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

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:

确定