在主类中找不到符号(帮助,很多混淆D :)

时间:2016-03-13 20:24:08

标签: java

因此,在我的课堂上,我必须编写一个程序,询问用户猫狗的名字和比赛并输出结果。它主要完成到最后一部分。

package l3c3computerscience;
public class L3C3ComputerScience {
    public static void main(String[] args) 
    {
        Cat myCat = new Cat();
        Dog myDog = new Dog();

        myCat.createCat(); //error is here
        myDog.createDog(); //error is also here
    }

}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package l3c3computerscience;

import java.util.Scanner;

/**
 *
 * @author
 */
public class Mammal {
    public Scanner input;
    public Dog myDog;
    public Cat myCat;
    public void createCat()
    {
        input = new Scanner(System.in);
        myCat = new Cat();
        String name;
        System.out.println("What is this cat's name?");
        name = input.next();
        myCat.setName(name);
        String race;
        System.out.println("What is this cat's race?");
        race = input.next();
        myCat.setRace(race);
    }
        public void createDog()
    {
        input = new Scanner(System.in);
        myDog = new Dog();
        String name;
        System.out.println("What is this dog's name?");
        name = input.next();
        myDog.setName(name);
        String race;
        System.out.println("What is this dog's race?");
        race = input.next();
        myDog.setRace(race);
    }
        public void display()
        {
            System.out.println("I have a "+myCat.getRace()+" cat and its name is "+myCat.getName());
            System.out.println("I have a "+myDog.getRace()+" dog and its name is "+myDog.getName());
        }

}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package l3c3computerscience;

/**
 *
 * @author Xander
 */
public class Cat 
{
    private String name;
    private String race;
    public Cat()
    {
        this.name = name;
        this.race = race;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public void setRace(String race)
    {
        this.race = race;
    }
    public String getName()
    {
        return this.name;
    }
        public String getRace()
    {
        return this.race;
    }
    public void display()
    {
        System.out.println("The cat's name is" + name);
        System.out.println("The cat's race is" + race);
    }
}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package l3c3computerscience;

/**
 *
 */
public class Dog 
{
    private String name;
    private String race;
    public Dog()
    {
        this.name = name;
        this.race = race;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public void setRace(String race)
    {
        this.race = race;
    }
    public String getName()
    {
        return name;
    }
        public String getRace()
    {
        return race;
    }
    public void display()
    {
        System.out.println("The dog's name is" + name);
        System.out.println("The dog's race is" + race);
    }
}

它不断给我,在第一部分下找不到符号。 “createCat /狗();”有下划线说“找不到符号”无法弄清楚原因,:(请帮帮我......

2 个答案:

答案 0 :(得分:1)

我不想这么说,但是你当前的程序设计已经关闭了:

  • 首先,Cat和Dog都应该从Mammal延伸
  • 但话说回来,Mammal需要完全重写 - 它应该没有用户界面代码,没有Scanner,没有println,而应该有状态(字段)和行为(方法) )狗和猫会分享。
  • 您的Cat和Dog类可能应该具有非默认构造函数,应该具有 参数 的构造函数,以允许设置字段。您当前使用this.name = name;等调用的构造函数会将字段设置为null值,因为这些构造函数没有参数。

e.g。

public Dog(String name, String race) {
    this.name = name;
    this.race = race;
}
  • 用户界面代码,使用Scanner并且其中包含printlns的代码应该在其他地方,您可能只需在主方法中使用此代码,无论您将它放置在何处,例如L3C3ComputerScience类。
  • 也许你会在L3C3ComputerScience中为createCat和createDog使用静态方法,但要确保这些方法不是void方法,而是分别返回Cat或Dog对象。
  • 考虑为这些方法提供Scanner参数,以便main方法可以将Scanner传递给方法以供其使用。

如,

public static Dog createDog(Scanner input) {
    // code to interact with user and create a dog
    String name = // ..... use input to get name from user
    String race = // ..... use input to get race from user

    // use the new constructor you've created that takes parameters
    Dog dog = new Dog(name, race);     

    // return the Dog from the method to the caller
    return dog;  
}

同样适用于createCat

答案 1 :(得分:-1)

根据您的要求:

首先,您需要在Mammal方法中创建main类对象,因为createCat()createDog()方法属于Mammal类。

其次,你将使用Mammal类,并且它位于。{ 名为l3c3computerscience的包,因此导入l3c3computerscience.Mammal;

最后,您的L3C3ComputerScience.java文件如下所示:

import l3c3computerscience.Mammal;

public class L3C3ComputerScience {

    public static void main(String[] args) {
        Mammal mammal = new Mammal();
        mammal.createCat();
        mammal.createDog();
        mammal.display();
    }
}

按顺序运行:

javac -d . Cat.java  
javac -d . Dog.java  
javac -d . Mammal.java  

javac L3C3ComputerScience.java  
java L3C3ComputerScience  

希望,它将满足您的要求。