为什么编译器对获取方法说“找不到符号”?

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

Why is the compiler saying "cannot find symbol" to the getter methods?

问题

当我尝试编译代码时,每次调用getter方法时都会持续报错“找不到符号”。我很乐意接受关于如何解决问题的任何建议。

以下是带有主方法的代码:

  1. import java.util.Scanner;
  2. public class Assignment10
  3. {
  4. public static void main(String [] args)
  5. {
  6. Scanner in = new Scanner(System.in);
  7. System.out.println("\nThis program displays some attributes and behaviors of two different dogs.");
  8. // 创建两个 Dogs 对象
  9. Dogs firstDog = new Dogs();
  10. Dogs secondDog = new Dogs();
  11. // 第一个狗的命名方案
  12. System.out.print("\nWhat would you like to name the first dog? ");
  13. firstDog.setName(in.nextLine());
  14. // 第二个狗的命名方案
  15. System.out.print("What would you like to name the second dog? ");
  16. secondDog.setName(in.nextLine());
  17. // 获取第一个狗的品种
  18. System.out.print("\nWhat is the breed of the first dog? ");
  19. firstDog.setBreed(in.nextLine());
  20. // 获取第二个狗的品种
  21. System.out.print("What is the breed of the second dog? ");
  22. secondDog.setBreed(in.nextLine());
  23. // 获取第一个狗的年龄
  24. System.out.println("\nWhere is the first dog in their lifespan? ");
  25. System.out.print("Enter 1 for puppy, 2 for adolescent, 3 for adult, 4 for senior: ");
  26. firstDog.setAge(in.nextInt());
  27. // 获取第二个狗的年龄
  28. System.out.println("Where is the second dog in their lifespan? ");
  29. secondDog.setAge(in.nextInt());
  30. // 获取第一个狗的体重
  31. System.out.println("\nWhere is the first dog in the weight range?");
  32. System.out.print("Enter 1 for low, 2 for medium, 3 for high: ");
  33. firstDog.setWeight(in.nextInt());
  34. // 获取第二个狗的体重
  35. System.out.println("Where is the second dog in the weight range?: ");
  36. secondDog.setWeight(in.nextInt());
  37. System.out.println("\nThank you for your input.");
  38. System.out.println("The following describes the first dog:\n");
  39. // 显示第一个狗的属性和行为
  40. System.out.println(firstDog.getName() + " is a " + firstDog.getAge() + " month old " + firstDog.getWeight() + " pound " + firstDog.getGender() + " " + firstDog.getBreed() + " who " + firstDog.getFleas() + "fleas.");
  41. System.out.print("When their owner tossed over a doggie treat, " + firstDog.getName() + " jumped in the air and went ");
  42. firstDog.eating();
  43. System.out.println();
  44. System.out.print("When " + firstDog.getName() + " ran back to their owner after fetching the ball, the " + firstDog.getBreed() + " dropped the ball and elatedly went ");
  45. firstDog.barking();
  46. System.out.println();
  47. if (firstDog.getFleas().equals("has"))
  48. {
  49. System.out.print("After rolling around in the mud, " + firstDog.getName() + " always goes ");
  50. firstDog.scratchingFleas();
  51. }
  52. // 显示第二个狗的属性和行为
  53. System.out.println(secondDog.getName() + " is a " + secondDog.getAge() + " month old " + secondDog.getWeight() + " pound " + secondDog.getGender() + " " + secondDog.getBreed() + " who " + secondDog.getFleas() + "fleas.");
  54. System.out.print(secondDog.getName() + " loudly goes ");
  55. secondDog.eating();
  56. System.out.println(" whenever they eat.");
  57. System.out.print(secondDog.getName() + " goes ");
  58. secondDog.barking();
  59. System.out.println(" each and every time there's a squirrel in the backyard.");
  60. if (secondDog.getFleas().equals("has"))
  61. {
  62. System.out.print("The owners brought the " + secondDog.getBreed() + " to the vet because " + secondDog.getName() + " kept going ");
  63. secondDog.scratchingFleas();
  64. System.out.print(" as if there were fleas.");
  65. }
  66. }
  67. }

这是定义对象的类的代码:

  1. public class Dogs
  2. {
  3. private StringBuffer z = new StringBuffer("");
  4. private StringBuffer name;
  5. private StringBuffer breed;
  6. private String gender;
  7. private int age;
  8. private double weight;
  9. private String fleas;
  10. private int i = (int)(Math.random() * 20);
  11. private int j = (int)(Math.random() * 20);
  12. private int k = (int)(Math.random() * 50);
  13. private int l = (int)(Math.random() * 50);
  14. public Dogs()
  15. {
  16. name = z;
  17. breed = z;
  18. gender = (i <= j) ? "female" : "male";
  19. age = 0;
  20. weight = 0;
  21. fleas = (k <= l) ? "has " : "does not have ";
  22. }
  23. public void setName(String s) {
  24. name = name.append(s);
  25. }
  26. public void setBreed(String s) {
  27. breed = breed.append(s);
  28. }
  29. public void setAge(int i)
  30. {
  31. if (i == 1)
  32. age = (int)(1 + Math.random() * 7);
  33. if (i == 2)
  34. age = (int)(8 + Math.random() * 10);
  35. if (i == 3)
  36. age = (int)(18 + Math.random() * 66);
  37. if (i == 4)
  38. age = (int)(84 + Math.random() * 49);
  39. }
  40. public void setWeight(int i)
  41. {
  42. if (i == 1)
  43. weight = 10 + Math.random() * 30;
  44. if (i == 2)
  45. weight = 40 + Math.random() * 60;
  46. if (i == 3)
  47. weight = 100 + Math.random() * 50;
  48. }
  49. public String getName() {
  50. return name.toString();
  51. }
  52. public int getAge() {
  53. return age;
  54. }
  55. public double getWeight() {
  56. return weight;
  57. }
  58. public String getGender() {
  59. return gender;
  60. }
  61. public String getBreed() {
  62. return breed.toString();
  63. }
  64. public String getFleas() {
  65. return fleas;
  66. }
  67. public void eating() {
  68. System.out.print("chomp chomp chomp!");
  69. }
  70. public void barking() {
  71. System.out.print("woof woof woof!");
  72. }
  73. public void scratchingFleas() {
  74. System.out.print("scrhh scrhh scrhh");
  75. }
  76. }

我非常感谢每个帮助我的人!!!

英文:

When I try to compile the code, it keeps saying "cannot find symbol" every single time I try to call a getter method. I'd love any and all suggestions as to how to fix the problem.

Here is the code with the main method

  1. import java.util.Scanner;
  2. public class Assignment10
  3. {
  4. public static void main(String [] args)
  5. {
  6. Scanner in = new Scanner(System.in);
  7. System.out.println(&quot;\nThis program displays some attributes and behaviors of two different dogs.&quot;);
  8. //Create two Dogs objects
  9. Dogs firstDog = new Dogs();
  10. Dogs secondDog = new Dogs();
  11. //Naming scheme for first dog
  12. System.out.print(&quot;\nWhat would you like to name the first dog? &quot;);
  13. firstDog.setName(in.nextLine());
  14. //Naming scheme for second dog
  15. System.out.print(&quot;What would you like to name the second dog? &quot;);
  16. secondDog.setName(in.nextLine());
  17. //Scheme for getting the breed of first dog
  18. System.out.print(&quot;\nWhat is the breed of the first dog? &quot;);
  19. firstDog.setBreed(in.nextLine());
  20. //Scheme for getting the breed of first dog
  21. System.out.print(&quot;What is the breed of the second dog? &quot;);
  22. secondDog.setBreed(in.nextLine());
  23. //Scheme to get age of first dog
  24. System.out.println(&quot;\nWhere is the first dog in their lifespan? &quot;);
  25. System.out.print(&quot;Enter 1 for puppy, 2 for adolescent, 3 for adult, 4 for senior: &quot;);
  26. firstDog.setAge(in.nextInt());
  27. //Scheme to get age of second dog
  28. System.out.println(&quot;Where is the first dog in their lifespan? &quot;);
  29. secondDog.setAge(in.nextInt());
  30. //Scheme to get weight of first dog
  31. System.out.println(&quot;\nWhere is the first dog in the weight range?&quot;);
  32. System.out.print(&quot;Enter 1 for low, 2 for medium, 3 for high: &quot;);
  33. firstDog.setWeight(in.nextInt());
  34. //Scheme to get weight of second dog
  35. System.out.println(&quot;Where is the second dog in the weight range?: &quot;);
  36. secondDog.setWeight(in.nextInt());
  37. System.out.println(&quot;\nThank you for your input.&quot;);
  38. System.out.println(&quot;The following describes the first dog:\n&quot;);
  39. //Displaying the attributes and behaviors of the first dog
  40. System.out.println( firstDog.getName + &quot; is a &quot; + firstDog.getAge + &quot; month old &quot; + firstDog.getWeight + &quot; pound &quot; + firstDog.getGender + &quot; &quot; + firstDog.getBreed + &quot; who &quot; + firstDog.getFleas + &quot;fleas.&quot;);
  41. System.out.print(&quot;When their owner tossed over a doggie treat, &quot; + firstDog.getName + &quot; jumped in the air and went &quot;);
  42. firstDog.eating();
  43. System.out.println();
  44. System.out.print(&quot;When &quot; + firstDog.getName + &quot; ran back to their owner after fetching the ball, the &quot; + firstDog.getBreed + &quot; dropped the ball and elatedly went &quot;);
  45. firstDog.barking();
  46. System.out.println();
  47. if ( firstDog.getFleas().equals(&quot;has&quot;) )
  48. {
  49. System.out.print(&quot;After rolling around in the mud, &quot; + firstDog.getName + &quot; always goes &quot;);
  50. firstDog.scratchingFleas();
  51. }
  52. //Displaying the attributes and behaviors of the second dog
  53. System.out.println( secondDog.getName + &quot; is a &quot; + secondDog.getAge + &quot; month old &quot; + secondDog.getWeight + &quot; pound &quot; + secondDog.getGender + &quot; &quot; + secondDog.getBreed + &quot; who &quot; + secondDog.getFleas + &quot;fleas.&quot;);
  54. System.out.print( secondDog.getName + &quot; loudly goes &quot;);
  55. secondDog.eating();
  56. System.out.println(&quot; whenever they eat.&quot;);
  57. System.out.print( secondDog.getName + &quot; goes &quot;);
  58. secondDog.barking();
  59. System.out.println(&quot; each and every time there&#39;s a squirrel in the backyard.&quot;);
  60. if ( secondDog.getFleas().equals(&quot;has&quot;) )
  61. {
  62. System.out.print(&quot;The owners brought the &quot; + secondDog.getBreed + &quot; to the vet because &quot; + secondDog.getName + &quot; kept going &quot;);
  63. secondDog.scratchingFleas();
  64. System.out.print(&quot; as if there were fleas.&quot;);
  65. }
  66. }

}

and here is the code with the class that defines the objects

  1. public class Dogs
  2. {
  3. private StringBuffer z = new StringBuffer(&quot;&quot;);
  4. private StringBuffer name;
  5. private StringBuffer breed;
  6. private String gender;
  7. private int age;
  8. private double weight;
  9. private String fleas;
  10. private int i = (int)(Math.random() * 20);
  11. private int j = (int)(Math.random() * 20);
  12. private int k = (int)(Math.random() * 50);
  13. private int l = (int)(Math.random() * 50);
  14. public Dogs()
  15. {
  16. name = z;
  17. breed = z;
  18. gender = (i &lt;= j) ? &quot;female&quot; : &quot;male&quot;;
  19. age = 0;
  20. weight = 0;
  21. fleas = (k &lt;= l) ? &quot;has &quot; : &quot;does not have &quot;;
  22. }
  23. public void setName(String s) {
  24. name = name.append(s);
  25. }
  26. public void setBreed(String s) {
  27. breed = breed.append(s);
  28. }
  29. public void setAge(int i)
  30. {
  31. if (i == 1)
  32. age = (int)(1 + Math.random() * 7);
  33. if (i == 2)
  34. age = (int)(8 + Math.random() * 10);
  35. if (i == 3)
  36. age = (int)(18 + Math.random() * 66);
  37. if (i == 4)
  38. age = (int)(84 + Math.random() * 49);
  39. }
  40. public void setWeight(int i)
  41. {
  42. if (i == 1)
  43. weight = 10 + Math.random() * 30;
  44. if (i == 2)
  45. weight = 40 + Math.random() * 60;
  46. if (i == 3)
  47. weight = 100 + Math.random() * 50;
  48. }
  49. public String getName() {
  50. return name.toString();
  51. }
  52. public int getAge() {
  53. return age;
  54. }
  55. public double getWeight() {
  56. return weight;
  57. }
  58. public String getGender() {
  59. return gender;
  60. }
  61. public String getBreed() {
  62. return breed.toString();
  63. }
  64. public String getFleas() {
  65. return fleas;
  66. }
  67. public void eating() {
  68. System.out.print(&quot;chomp chomp chomp!&quot;);
  69. }
  70. public void barking() {
  71. System.out.print(&quot;woof woof woof!&quot;);
  72. }
  73. public void scratchingFleas() {
  74. System.out.print(&quot;scrhh scrhh scrhh&quot;);
  75. }
  76. }

I really appreciate everyone who helps!!!!

答案1

得分: 3

你没有调用这些getter方法。你写成了dog.getName,但你应该写成dog.getName();

英文:

You are not calling the getters. You do dog.getNamewhen you should be doing dog.getName();

huangapple
  • 本文由 发表于 2020年8月21日 18:22:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63521027.html
匿名

发表评论

匿名网友

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

确定