为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕田峪7331174 2022-09-21 21:32:18
我有一个字符串,当输入超出指定值时,该字符串被设置为默认值,但它不断返回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);    }
查看完整描述

3 回答

?
杨魅力

TA贡献1811条经验 获得超5个赞

你的家猫有两个构造器:


// 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";


同样的事情发生在 - 你用3个参数调用构造函数,而不是设置为默认值的无参数构造函数。Catmood


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


// in HouseCat

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


// in Cat

public String mood = "Sleepy";


查看完整回答
反对 回复 2022-09-21
?
白衣染霜花

TA贡献1796条经验 获得超10个赞

当您创建名为参数化的对象时,在默认构造函数中,您初始化了这些对象,这就是为什么这些是空的。HouseCatConstructorTypeMood


您需要在参数化中设置这些值,然后它们将显示您的显式输出。像构造函数 sholud 一样被修改ConstructorHousecat


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

        }

    } 


查看完整回答
反对 回复 2022-09-21
?
慕标琳琳

TA贡献1830条经验 获得超9个赞

您在 Cat 类中创建了两个构造函数:


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.将未使用的构造函数更改为“简单”方法,您将在构造函数 3 中立即调用该方法。...super


例:


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

        }

    }


查看完整回答
反对 回复 2022-09-21
  • 3 回答
  • 0 关注
  • 142 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信