我有一个字符串,当输入超出指定值时,该字符串被设置为默认值,但它一直返回 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";
当您创建 Object of HouseCat
时,您调用了参数化Constructor
并在默认构造函数中初始化了 Type
并Mood
这就是为什么这些是空的。
您需要在参数化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 类中创建了两个构造函数:
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. 将未使用的构造函数更改为"简单"方法,您将在构造函数中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");
}
}
哼!