亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我的輸出為 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條經驗 獲得超6個贊

你的家貓有兩個構造器:


// 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 關注
  • 304 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號