英文:
Why is the compiler saying "cannot find symbol" to the getter methods?
问题
当我尝试编译代码时,每次调用getter方法时都会持续报错“找不到符号”。我很乐意接受关于如何解决问题的任何建议。
以下是带有主方法的代码:
import java.util.Scanner;
public class Assignment10
{
public static void main(String [] args)
{
Scanner in = new Scanner(System.in);
System.out.println("\nThis program displays some attributes and behaviors of two different dogs.");
// 创建两个 Dogs 对象
Dogs firstDog = new Dogs();
Dogs secondDog = new Dogs();
// 第一个狗的命名方案
System.out.print("\nWhat would you like to name the first dog? ");
firstDog.setName(in.nextLine());
// 第二个狗的命名方案
System.out.print("What would you like to name the second dog? ");
secondDog.setName(in.nextLine());
// 获取第一个狗的品种
System.out.print("\nWhat is the breed of the first dog? ");
firstDog.setBreed(in.nextLine());
// 获取第二个狗的品种
System.out.print("What is the breed of the second dog? ");
secondDog.setBreed(in.nextLine());
// 获取第一个狗的年龄
System.out.println("\nWhere is the first dog in their lifespan? ");
System.out.print("Enter 1 for puppy, 2 for adolescent, 3 for adult, 4 for senior: ");
firstDog.setAge(in.nextInt());
// 获取第二个狗的年龄
System.out.println("Where is the second dog in their lifespan? ");
secondDog.setAge(in.nextInt());
// 获取第一个狗的体重
System.out.println("\nWhere is the first dog in the weight range?");
System.out.print("Enter 1 for low, 2 for medium, 3 for high: ");
firstDog.setWeight(in.nextInt());
// 获取第二个狗的体重
System.out.println("Where is the second dog in the weight range?: ");
secondDog.setWeight(in.nextInt());
System.out.println("\nThank you for your input.");
System.out.println("The following describes the first dog:\n");
// 显示第一个狗的属性和行为
System.out.println(firstDog.getName() + " is a " + firstDog.getAge() + " month old " + firstDog.getWeight() + " pound " + firstDog.getGender() + " " + firstDog.getBreed() + " who " + firstDog.getFleas() + "fleas.");
System.out.print("When their owner tossed over a doggie treat, " + firstDog.getName() + " jumped in the air and went ");
firstDog.eating();
System.out.println();
System.out.print("When " + firstDog.getName() + " ran back to their owner after fetching the ball, the " + firstDog.getBreed() + " dropped the ball and elatedly went ");
firstDog.barking();
System.out.println();
if (firstDog.getFleas().equals("has"))
{
System.out.print("After rolling around in the mud, " + firstDog.getName() + " always goes ");
firstDog.scratchingFleas();
}
// 显示第二个狗的属性和行为
System.out.println(secondDog.getName() + " is a " + secondDog.getAge() + " month old " + secondDog.getWeight() + " pound " + secondDog.getGender() + " " + secondDog.getBreed() + " who " + secondDog.getFleas() + "fleas.");
System.out.print(secondDog.getName() + " loudly goes ");
secondDog.eating();
System.out.println(" whenever they eat.");
System.out.print(secondDog.getName() + " goes ");
secondDog.barking();
System.out.println(" each and every time there's a squirrel in the backyard.");
if (secondDog.getFleas().equals("has"))
{
System.out.print("The owners brought the " + secondDog.getBreed() + " to the vet because " + secondDog.getName() + " kept going ");
secondDog.scratchingFleas();
System.out.print(" as if there were fleas.");
}
}
}
这是定义对象的类的代码:
public class Dogs
{
private StringBuffer z = new StringBuffer("");
private StringBuffer name;
private StringBuffer breed;
private String gender;
private int age;
private double weight;
private String fleas;
private int i = (int)(Math.random() * 20);
private int j = (int)(Math.random() * 20);
private int k = (int)(Math.random() * 50);
private int l = (int)(Math.random() * 50);
public Dogs()
{
name = z;
breed = z;
gender = (i <= j) ? "female" : "male";
age = 0;
weight = 0;
fleas = (k <= l) ? "has " : "does not have ";
}
public void setName(String s) {
name = name.append(s);
}
public void setBreed(String s) {
breed = breed.append(s);
}
public void setAge(int i)
{
if (i == 1)
age = (int)(1 + Math.random() * 7);
if (i == 2)
age = (int)(8 + Math.random() * 10);
if (i == 3)
age = (int)(18 + Math.random() * 66);
if (i == 4)
age = (int)(84 + Math.random() * 49);
}
public void setWeight(int i)
{
if (i == 1)
weight = 10 + Math.random() * 30;
if (i == 2)
weight = 40 + Math.random() * 60;
if (i == 3)
weight = 100 + Math.random() * 50;
}
public String getName() {
return name.toString();
}
public int getAge() {
return age;
}
public double getWeight() {
return weight;
}
public String getGender() {
return gender;
}
public String getBreed() {
return breed.toString();
}
public String getFleas() {
return fleas;
}
public void eating() {
System.out.print("chomp chomp chomp!");
}
public void barking() {
System.out.print("woof woof woof!");
}
public void scratchingFleas() {
System.out.print("scrhh scrhh scrhh");
}
}
我非常感谢每个帮助我的人!!!
英文:
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
import java.util.Scanner;
public class Assignment10
{
public static void main(String [] args)
{
Scanner in = new Scanner(System.in);
System.out.println("\nThis program displays some attributes and behaviors of two different dogs.");
//Create two Dogs objects
Dogs firstDog = new Dogs();
Dogs secondDog = new Dogs();
//Naming scheme for first dog
System.out.print("\nWhat would you like to name the first dog? ");
firstDog.setName(in.nextLine());
//Naming scheme for second dog
System.out.print("What would you like to name the second dog? ");
secondDog.setName(in.nextLine());
//Scheme for getting the breed of first dog
System.out.print("\nWhat is the breed of the first dog? ");
firstDog.setBreed(in.nextLine());
//Scheme for getting the breed of first dog
System.out.print("What is the breed of the second dog? ");
secondDog.setBreed(in.nextLine());
//Scheme to get age of first dog
System.out.println("\nWhere is the first dog in their lifespan? ");
System.out.print("Enter 1 for puppy, 2 for adolescent, 3 for adult, 4 for senior: ");
firstDog.setAge(in.nextInt());
//Scheme to get age of second dog
System.out.println("Where is the first dog in their lifespan? ");
secondDog.setAge(in.nextInt());
//Scheme to get weight of first dog
System.out.println("\nWhere is the first dog in the weight range?");
System.out.print("Enter 1 for low, 2 for medium, 3 for high: ");
firstDog.setWeight(in.nextInt());
//Scheme to get weight of second dog
System.out.println("Where is the second dog in the weight range?: ");
secondDog.setWeight(in.nextInt());
System.out.println("\nThank you for your input.");
System.out.println("The following describes the first dog:\n");
//Displaying the attributes and behaviors of the first dog
System.out.println( firstDog.getName + " is a " + firstDog.getAge + " month old " + firstDog.getWeight + " pound " + firstDog.getGender + " " + firstDog.getBreed + " who " + firstDog.getFleas + "fleas.");
System.out.print("When their owner tossed over a doggie treat, " + firstDog.getName + " jumped in the air and went ");
firstDog.eating();
System.out.println();
System.out.print("When " + firstDog.getName + " ran back to their owner after fetching the ball, the " + firstDog.getBreed + " dropped the ball and elatedly went ");
firstDog.barking();
System.out.println();
if ( firstDog.getFleas().equals("has") )
{
System.out.print("After rolling around in the mud, " + firstDog.getName + " always goes ");
firstDog.scratchingFleas();
}
//Displaying the attributes and behaviors of the second dog
System.out.println( secondDog.getName + " is a " + secondDog.getAge + " month old " + secondDog.getWeight + " pound " + secondDog.getGender + " " + secondDog.getBreed + " who " + secondDog.getFleas + "fleas.");
System.out.print( secondDog.getName + " loudly goes ");
secondDog.eating();
System.out.println(" whenever they eat.");
System.out.print( secondDog.getName + " goes ");
secondDog.barking();
System.out.println(" each and every time there's a squirrel in the backyard.");
if ( secondDog.getFleas().equals("has") )
{
System.out.print("The owners brought the " + secondDog.getBreed + " to the vet because " + secondDog.getName + " kept going ");
secondDog.scratchingFleas();
System.out.print(" as if there were fleas.");
}
}
}
and here is the code with the class that defines the objects
public class Dogs
{
private StringBuffer z = new StringBuffer("");
private StringBuffer name;
private StringBuffer breed;
private String gender;
private int age;
private double weight;
private String fleas;
private int i = (int)(Math.random() * 20);
private int j = (int)(Math.random() * 20);
private int k = (int)(Math.random() * 50);
private int l = (int)(Math.random() * 50);
public Dogs()
{
name = z;
breed = z;
gender = (i <= j) ? "female" : "male";
age = 0;
weight = 0;
fleas = (k <= l) ? "has " : "does not have ";
}
public void setName(String s) {
name = name.append(s);
}
public void setBreed(String s) {
breed = breed.append(s);
}
public void setAge(int i)
{
if (i == 1)
age = (int)(1 + Math.random() * 7);
if (i == 2)
age = (int)(8 + Math.random() * 10);
if (i == 3)
age = (int)(18 + Math.random() * 66);
if (i == 4)
age = (int)(84 + Math.random() * 49);
}
public void setWeight(int i)
{
if (i == 1)
weight = 10 + Math.random() * 30;
if (i == 2)
weight = 40 + Math.random() * 60;
if (i == 3)
weight = 100 + Math.random() * 50;
}
public String getName() {
return name.toString();
}
public int getAge() {
return age;
}
public double getWeight() {
return weight;
}
public String getGender() {
return gender;
}
public String getBreed() {
return breed.toString();
}
public String getFleas() {
return fleas;
}
public void eating() {
System.out.print("chomp chomp chomp!");
}
public void barking() {
System.out.print("woof woof woof!");
}
public void scratchingFleas() {
System.out.print("scrhh scrhh scrhh");
}
}
I really appreciate everyone who helps!!!!
答案1
得分: 3
你没有调用这些getter方法。你写成了dog.getName
,但你应该写成dog.getName();
。
英文:
You are not calling the getters. You do dog.getName
when you should be doing dog.getName();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论