到目前为止,我的代码如下所示:
public class CatWorld {
public static void main (String[] args) {
Scanner getLine = new Scanner(System.in);
String userSays;
//ARRAY:
int [] CatArray;
CatArray = new int [5];
//ARRAY-POWERED LOOP:
for (int i=0; i < CatArray.length; i ++) {
//OPTIONAL PROMPT:
System.out.println ("Wow! A brand new cat! What's its name?");
//Mandatory below
userSays = getLine.next();
Cat babyCat = new Cat(userSays);
System.out.println ("The new cat's name is "
+ babyCat.getcatName() + "!");
}
}
}
我的构造函数如下所示:
public class Cat {
String catName = "Billybob";
public Cat (String Name) { //Can also be birthName
catName = Name;
}
public String getcatName(){
return catName;
}
}
当我运行它时会发生什么,它会在我输入名称后立即输出。在输入 5 个名称后,我将如何将它们全部输出?