我的代碼看起來像這樣至今:如何在java中的循環用戶輸入後打印出來?
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個名稱輸入後輸出它們?
澄清一下,我該怎麼辦記得循環結束後所有小貓的名字? –