我的输出结果为 Null 而不是使用字符串的默认值

My output comes out Null instead of using the default value for strings

当输入超出指定值时,我有一个字符串被设置为默认值,但它一直返回 null 而不是默认值。我缺少什么代码才能获得正确的输出?

我发现我的整数和双精度值正确使用了默认值,但我的字符串却没有。

//这是我前端的剪辑

import java.util.Scanner;
public class AnimalFrontEnd {
    public static final int ARRAY_SIZE = 10;
    Scanner keyboard = new Scanner(System.in) ;

    public static void main(String[] args) {
        boolean cont = true;
        int input = 0;
        int type = 0;
        AnimalCollection collection = new AnimalCollection(ARRAY_SIZE);
        Scanner keyboard = new Scanner(System.in) ;
        String rName = "";
        System.out.println("Welcome to the Cat and Dog Collection");
        while(cont) {
            System.out.println("1. Add a cat or dog \n2. Remove a cat or dog \n3. Quit \nEnter a selection");
            input = Integer.parseInt(keyboard.nextLine());
            switch(input) {
            case 1:
                System.out.println("Would you like to add \n1. A House Cat \n2. A Leopard \n3. A Domestic Dog \n4. A Wolf");
                type = Integer.parseInt(keyboard.nextLine());
                switch(type) {
                case 1:
                    HouseCat kitty = getHCat();
                    collection.addAnimal(kitty);
                    break;

//在我前端再往下

private static HouseCat getHCat() {
        String name;
        double weight;
        String mood;
        String cType;

        Scanner keyboard = new Scanner(System.in) ;
        System.out.println("Enter the cat's name, weight, mood, and type");
        name = keyboard.nextLine();
        weight = Double.parseDouble(keyboard.nextLine());
        mood = keyboard.nextLine();
        cType = keyboard.nextLine();
        return new HouseCat(name, weight, mood, cType);
    }

//我的摘要class

public abstract class Animal {
    public String name;
    public double weight;

    Animal(){
        this.name = "Cuddles";
        this.weight = 0;
    }
    public Animal(String name, double weight) {
        this.setName(name);
        if(weight>0) {
            this.setWeight(weight);
        }
        else {
            System.out.println("Invalid weight");
        }
    }
    public String getAName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    public String toString() {
        return "Name: "+name+" Weight: "+weight;
    }
}

//动物的扩展

public class Cat extends Animal {
    public String mood;

    Cat(){
        this.mood = "Sleepy";
    }
    public Cat(String name, double weight, String mood) {
        super(name, weight);
        if(mood.equalsIgnoreCase("sleepy")||mood.equalsIgnoreCase("playful")||mood.equalsIgnoreCase("hungry")) {
            this.setMood(mood);
        }
        else {
            System.out.println("Invalid mood");
        }
    }
    public String getMood() {
        return mood;
    }
    public void setMood(String mood) {
        this.mood = mood;
    }
    public String toString() {
        return super.toString()+" Mood: "+mood;
    }
}

//从Cat

延伸出来的class
public class HouseCat extends Cat {
    public String cType;

    HouseCat(){
        this.cType = "Short Hair";
    }
    public HouseCat(String name, double weight, String mood, String cType) {
        super(name, weight, mood);
        if(cType.equalsIgnoreCase("Short Hair")||cType.equalsIgnoreCase("Bombay")||cType.equalsIgnoreCase("Ragdoll")||cType.equalsIgnoreCase("Sphinx")||cType.equalsIgnoreCase("Scottish Fold")) {
            this.setCType(cType);
        }
        else {
            System.out.println("Invalid type");
        }
    }
    public String getCType(){
        return cType;
    }
    public void setCType(String cType) {
        this.cType = cType;
    }
    public String toString() {
        return super.toString()+" Type: "+cType;
    }
}


我希望出现的:

Welcome to the Cat and Dog Collection
1. Add a cat or dog 
2. Remove a cat or dog 
3. Quit 
Enter a selection
1
Would you like to add 
1. A House Cat 
2. A Leopard 
3. A Domestic Dog 
4. A Wolf
1
Enter the cat's name, weight, mood, and type
Booph
23
unhappy
maine coon
Invalid mood
Invalid type
Collection: Name: Booph Weight: 23.0 Mood: Sleepy Type: Short Hair

实际显示的内容:

Welcome to the Cat and Dog Collection
1. Add a cat or dog 
2. Remove a cat or dog 
3. Quit 
Enter a selection
1
Would you like to add 
1. A House Cat 
2. A Leopard 
3. A Domestic Dog 
4. A Wolf
1
Enter the cat's name, weight, mood, and type
Booph
23
unhappy
maine coon
Invalid mood
Invalid type
Collection: Name: Booph Weight: 23.0 Mood: null Type: null

你的家猫有两个构造器:

// one constructor
HouseCat(){
    this.cType = "Short Hair";
}

// another constructor
public HouseCat(String name, double weight, String mood, String cType) {
    super(name, weight, mood);
    if(cType.equalsIgnoreCase("Short Hair")||cType.equalsIgnoreCase("Bombay")||cType.equalsIgnoreCase("Ragdoll")||cType.equalsIgnoreCase("Sphinx")||cType.equalsIgnoreCase("Scottish Fold")) {
        this.setCType(cType);
    }
    else {
        System.out.println("Invalid type");
    }
}

您在前端调用带有 4 个参数的构造函数,而不是无参数构造函数,因此 this.cType = "Short Hair"; 从未得到 运行。

同样的事情发生在 Cat 中 - 您使用 3 个参数调用构造函数,而不是将 mood 设置为默认值的无参数参数。

要解决此问题,只需删除无参数构造函数并内联初始化变量:

// in HouseCat
public String cType = "Short Hair"; // btw you shouldn't use public fields.

// in Cat
public String mood = "Sleepy";

当您创建 HouseCat 的对象时,您调用了参数化的 Constructor 并且在您的默认构造函数中您初始化了 TypeMood 这就是为什么它们为空的原因。

您需要在参数化 Constructor 中设置这些值,然后它们将显示您的预期输出。像 Housecat 构造函数应该修改

public HouseCat(String name, double weight, String mood, String cType) {
        super(name, weight, mood);
        if(cType.equalsIgnoreCase("Short Hair")||cType.equalsIgnoreCase("Bombay")||cType.equalsIgnoreCase("Ragdoll")||cType.equalsIgnoreCase("Sphinx")||cType.equalsIgnoreCase("Scottish Fold")) {
            this.setCType(cType);
        }
        else {
            System.out.println("Invalid type");
            this.cType = "Short Hair";//<------------- set default value here
        }
    }

Cat 构造函数应该修改为

public Cat(String name, double weight, String mood) {
        super(name, weight);
        if(mood.equalsIgnoreCase("sleepy")||mood.equalsIgnoreCase("playful")||mood.equalsIgnoreCase("hungry")) {
            this.setMood(mood);
        }
        else {
            System.out.println("Invalid mood");
           this.mood = "Sleepy";//<-------------  set default value here
        }
    } 

您已经在 Cat class 中创建了两个构造函数:

Cat(){
        this.mood = "Sleepy";
    }

    public Cat(String name, double weight, String mood) {
        super(name, weight);
        if(mood.equalsIgnoreCase("sleepy")||mood.equalsIgnoreCase("playful")||mood.equalsIgnoreCase("hungry")) {
           this.setMood(mood);
        }
        else {
            System.out.println("Invalid mood");
        }
    }

只有第一个(没有参数)初始化情绪场。你显然使用另一个来创建你的实例...

你有多种解决方案: 1.删​​除未使用的构造函数并在另一个构造函数中初始化心情 2. 将未使用的构造函数更改为 'simple' 方法,您将在构造函数中的 super 之后立即调用该方法 3. ...

示例:

public Cat(String name, double weight, String mood) {
  super(name, weight);
  this.mood = "Sleepy";

 if(mood.equalsIgnoreCase("sleepy")||mood.equalsIgnoreCase("playful")||mood.equalsIgnoreCase("hungry")) {
            this.setMood(mood);
        }
        else {
            System.out.println("Invalid mood");
        }
    }

HTH!