狗类DogApp

时间:2014-10-12 19:06:07

标签: java

我需要制作两个Java类(Dog,DogApp)。 Dog类需要能够设置和获取名称,品种,颜色,重量,身高和长度。该类还必须在最后包含变量名称的bark语句。我有Dog类,但是在DogApp类中,我在getName,getBreed,getColor,getWeight,getHeight和getLength行上遇到语法错误。我究竟做错了什么? 任何帮助将不胜感激,我确实知道这个问题的版本是exhist但不一样 狗班:

public class Dog {
    private static Object sc;
    private String name;
    private String breed;
    private String color;
    private double weight;
    private double height;
    private double length;

   public Dog() {
       name = null;
       breed = null;
       color = null;
       weight = 0;
       height = 0;
       length = 0;
   }

 public static Object getSc() {
     return sc;
 }

 public static void setSc(Object sc) {
     Dog.sc = sc;
 }

 public String getName() {
     return name;
 }

 public void setName(String name) {
     this.name = name;
 }

 public String getBreed() {
     return breed;
 }

 public void setBreed(String breed) {
     this.breed = breed;
 }

 public String getColor() {
     return color;
 }

 public void setColor(String color) {
     this.color = color;
 }

 public double getWeight() {
     return weight;
 }

 public void setWeight(double weight) {
     this.weight = weight;
 }

 public double getHeight() {
     return height;
 }

 public void setHeight(double height) {
     this.height = height;
 }

 public double getLength() {
     return length;
 }

 public void setLength(double length) {
     this.length = length;
 }

 public String Bark(String bark) {
     bark = (name + " says woof.");
     return bark;
 }
}

******
DogApp:

 import java.util.Scanner;

public class DogApp {

private static String name;
private static String breed;
private static String color;
private static double weight;
private static double height;
private static double length;
private static String bark;

public static void main(String args[]) {
    // display a welcome message
    Scanner sc = new Scanner(System.in);
    String choice = "y";
    while (choice.equalsIgnoreCase("y")) {
        System.out.print("Enter Dog's information");
        System.out.println();
        Dog dog = new Dog();

        System.out.print("Enter dog's name: ");
        name = sc.nextLine();
        dog.setName(name);

        System.out.print("Enter Dog's breed: ");
        dog.setBreed(breed);
        breed = sc.nextLine();

        System.out.print("Enter Dog's color: ");
        dog.setColor(color);
        color = sc.nextLine();

        System.out.print("Enter Dog's weight: ");
        dog.setWeight(weight);
        weight = sc.nextDouble();

        System.out.print("Enter Dog's height: ");
        dog.setHeight(height);
        height = sc.nextDouble();

        System.out.print("Enter Dog's length: ");
        dog.setLength(length);
        length = sc.nextDouble();

        System.out.println();
        System.out.println("name:     " + (name));
        System.out.println("breed:    " + (breed));
        System.out.println("color:    " + (color));
        System.out.println("weight:   " + (weight));
        System.out.println("height:   " + (height));
        System.out.println("length:   " + (length));
        System.out.println(dog.getBark(bark));
        System.out.print("Continue?  y/n: ");
        choice = sc.next();

    }

  }
}

1 个答案:

答案 0 :(得分:2)

您正在静态调用这些方法。静态方法应保留用于获取方法签名中所需的所有信息的方法。您还交换了getter和setter方法。

尝试创建dog类型的对象,并为其分配数据:

    Scanner sc = new Scanner(System.in);
    Dog currentDog = new Dog();

    String name = sc.next();
    currentDog.setName(name);
    String breed = sc.next();
    currentDog.setBreed(breed);
    String color = sc.next();
    currentDog.setColor(color);
    double weight = sc.nextDouble();
    currentDog.setWeight(weight);
    double height = sc.nextDouble();
    currentDog.setHeight(height);
    double length = sc.nextDouble();
    currentDog.setLength(length);

然后检索您的数据:

    String name = currentDog.getName();
    String breed = currentDog.getBreed();
    String color = currentDog.getColor();
    double weight = currentDog.getWeight();
    double height = currentDog.getHeight();
    double length = currentDog.getLength();

对于方法...... Understanding Class Members

您应该研究的另一个资源是Scanner API Documentation 关于如何使用扫描仪对象。