为什么编译器对 getter 方法说 "cannot find symbol"?
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.");
//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.");
}
}
}
这是定义对象
的带有 class 的代码
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");
}
}
非常感谢大家的帮助!!!!
你没有调用吸气剂。你在应该做 dog.getName();
的时候做了 dog.getName
当我尝试编译代码时,每次我尝试调用 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.");
//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.");
}
}
}
这是定义对象
的带有 class 的代码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");
}
}
非常感谢大家的帮助!!!!
你没有调用吸气剂。你在应该做 dog.getName();
dog.getName