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

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

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(&quot;\nThis program displays some attributes and behaviors of two different dogs.&quot;);
//Create two Dogs objects
Dogs firstDog = new Dogs();
Dogs secondDog = new Dogs();
//Naming scheme for first dog
System.out.print(&quot;\nWhat would you like to name the first dog? &quot;);
firstDog.setName(in.nextLine());
//Naming scheme for second dog
System.out.print(&quot;What would you like to name the second dog? &quot;);
secondDog.setName(in.nextLine());
//Scheme for getting the breed of first dog
System.out.print(&quot;\nWhat is the breed of the first dog? &quot;);
firstDog.setBreed(in.nextLine());
//Scheme for getting the breed of first dog
System.out.print(&quot;What is the breed of the second dog? &quot;);
secondDog.setBreed(in.nextLine());
//Scheme to get age of first dog
System.out.println(&quot;\nWhere is the first dog in their lifespan? &quot;);
System.out.print(&quot;Enter 1 for puppy, 2 for adolescent, 3 for adult, 4 for senior: &quot;);
firstDog.setAge(in.nextInt());
//Scheme to get age of second dog
System.out.println(&quot;Where is the first dog in their lifespan? &quot;);
secondDog.setAge(in.nextInt());
//Scheme to get weight of first dog
System.out.println(&quot;\nWhere is the first dog in the weight range?&quot;);
System.out.print(&quot;Enter 1 for low, 2 for medium, 3 for high: &quot;);
firstDog.setWeight(in.nextInt());
//Scheme to get weight of second dog
System.out.println(&quot;Where is the second dog in the weight range?: &quot;);
secondDog.setWeight(in.nextInt());
System.out.println(&quot;\nThank you for your input.&quot;);
System.out.println(&quot;The following describes the first dog:\n&quot;);
//Displaying the attributes and behaviors of the first dog
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;);
System.out.print(&quot;When their owner tossed over a doggie treat, &quot; + firstDog.getName + &quot; jumped in the air and went &quot;);
firstDog.eating();
System.out.println();
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;);
firstDog.barking();
System.out.println();
if ( firstDog.getFleas().equals(&quot;has&quot;) )
{
System.out.print(&quot;After rolling around in the mud, &quot; + firstDog.getName + &quot; always goes &quot;);
firstDog.scratchingFleas();
}
//Displaying the attributes and behaviors of the second dog
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;);
System.out.print( secondDog.getName + &quot; loudly goes &quot;);
secondDog.eating();
System.out.println(&quot; whenever they eat.&quot;);
System.out.print( secondDog.getName + &quot; goes &quot;);
secondDog.barking();
System.out.println(&quot; each and every time there&#39;s a squirrel in the backyard.&quot;);
if ( secondDog.getFleas().equals(&quot;has&quot;) )
{
System.out.print(&quot;The owners brought the &quot; + secondDog.getBreed + &quot; to the vet because &quot; + secondDog.getName + &quot; kept going &quot;);
secondDog.scratchingFleas();
System.out.print(&quot; as if there were fleas.&quot;);
}
}

}

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

public class Dogs
{
private StringBuffer z = new StringBuffer(&quot;&quot;);
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 &lt;= j) ? &quot;female&quot; : &quot;male&quot;;
age = 0;
weight = 0;
fleas = (k &lt;= l) ? &quot;has &quot; : &quot;does not have &quot;;
}
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(&quot;chomp chomp chomp!&quot;);
}
public void barking() {
System.out.print(&quot;woof woof woof!&quot;);
}
public void scratchingFleas() {
System.out.print(&quot;scrhh scrhh scrhh&quot;);
}
}

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:

确定