java - 我的输出是 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);
}
//我的抽象类
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扩展的类
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";
推荐阅读
- java - 时间复杂度 - 将映射条目复制到 Arraylist
- discord.js - 达到特定级别时向用户添加角色的问题
- powerquery - 用电量查询填补时间空白
- c# - 如何更新 Word 文档中的正文和超链接
- angular - 如何在从 col-12 更改为 col-9 时向 css 中的 div 添加过渡效果?
- websocket - 向命名空间的 Socket.io 房间发出广播
- reporting-services - How to not COUNT a value in SSRS matrix when value is NULL
- c++ - 如何在 QStandardItemModel 中查找一行
- powerbi - 一列中有重复值,第二列中有不同值的行
- jquery - (Fullcalendar) 使用后端信息自动点击天数