我有一个字符串,当输入超出指定值时,该字符串被设置为默认值,但它不断返回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);
}
杨魅力
白衣染霜花
慕标琳琳
相关分类