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

我有一个字符串,当输入超出指定值时,该字符串被设置为默认值,但它不断返回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);

    }



慕田峪7331174
浏览 144回答 3
3回答

杨魅力

你的家猫有两个构造器:// one constructorHouseCat(){    this.cType = "Short Hair";}// another constructorpublic 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 HouseCatpublic String cType = "Short Hair"; // btw you shouldn't use public fields.// in Catpublic String mood = "Sleepy";

白衣染霜花

当您创建名为参数化的对象时,在默认构造函数中,您初始化了这些对象,这就是为什么这些是空的。HouseCatConstructorTypeMood您需要在参数化中设置这些值,然后它们将显示您的显式输出。像构造函数 sholud 一样被修改ConstructorHousecatpublic HouseCat(String name, double weight, String mood, String cType) {&nbsp; &nbsp; &nbsp; &nbsp; super(name, weight, mood);&nbsp; &nbsp; &nbsp; &nbsp; if(cType.equalsIgnoreCase("Short Hair")||cType.equalsIgnoreCase("Bombay")||cType.equalsIgnoreCase("Ragdoll")||cType.equalsIgnoreCase("Sphinx")||cType.equalsIgnoreCase("Scottish Fold")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.setCType(cType);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Invalid type");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.cType = "Short Hair";//<------------- set default value here&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }和构造函数应该像Catpublic Cat(String name, double weight, String mood) {&nbsp; &nbsp; &nbsp; &nbsp; super(name, weight);&nbsp; &nbsp; &nbsp; &nbsp; if(mood.equalsIgnoreCase("sleepy")||mood.equalsIgnoreCase("playful")||mood.equalsIgnoreCase("hungry")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.setMood(mood);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Invalid mood");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.mood = "Sleepy";//<-------------&nbsp; set default value here&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp;

慕标琳琳

您在 Cat 类中创建了两个构造函数:Cat(){&nbsp; &nbsp; &nbsp; &nbsp; this.mood = "Sleepy";&nbsp; &nbsp; }和&nbsp; &nbsp; public Cat(String name, double weight, String mood) {&nbsp; &nbsp; &nbsp; &nbsp; super(name, weight);&nbsp; &nbsp; &nbsp; &nbsp; if(mood.equalsIgnoreCase("sleepy")||mood.equalsIgnoreCase("playful")||mood.equalsIgnoreCase("hungry")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.setMood(mood);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Invalid mood");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }只有第一个(没有参数)初始化情绪字段。您显然使用另一个来创建您的实例...你有多个解决方案:1.删除未使用的构造函数并在另一个构造函数中引发情绪 2.将未使用的构造函数更改为“简单”方法,您将在构造函数 3 中立即调用该方法。...super例:public Cat(String name, double weight, String mood) {&nbsp; super(name, weight);&nbsp; this.mood = "Sleepy";&nbsp;if(mood.equalsIgnoreCase("sleepy")||mood.equalsIgnoreCase("playful")||mood.equalsIgnoreCase("hungry")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.setMood(mood);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Invalid mood");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java