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

ecr0jaav  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(405)

**结案。**此问题不可复制或由打字错误引起。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

5个月前关门了。
改进这个问题
当我试图编译代码时,每当我试图调用getter方法时,它总是说“找不到符号”。关于如何解决这个问题,我希望有任何建议。
下面是main方法的代码

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.");
    }
}

}
下面是定义对象的类的代码

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

我真的很感谢所有帮助我的人!!!!

ikfrs5lh

ikfrs5lh1#

你不是在给那些人打电话。你知道吗 dog.getName 你应该做什么 dog.getName();

相关问题