2

到目前为止,我的代码如下所示:

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 个名称后,我将如何将它们全部输出?

4

4 回答 4

1

您需要以Cat某种方式存储 s 。

List<Cat> catList = new ArrayList<Cat>();

// Ask for cat names, and insert cat objects into list

然后在最后,

for (Cat cat : catList) {
  // print whatever you want about the cats.
}
于 2013-10-18T01:49:22.973 回答
1

测试和工作

将方法中的代码更改main为:

Scanner getLine = new Scanner(System.in);
String userSays;

Cat[] catList = new Cat[5]; // create array of 5 cats

int catCount = 0;

// loop to get all the user input for the cats
for (Cat cat : catList) // for each cat in cat array (5 cats)
{
    System.out.println("Wow! A brand new cat! What's its name?");

    userSays = getLine.next(); // get the cat name

    catList[catCount] = new Cat(userSays); // set this cat in the cat array to be
                                           // the user input

    catCount++; // + the catCount, so the next cat in the cat array is focused on
}

// loop to display all of the cats back to the console
for (Cat cat : catList) // for each cat in the cat array
{
    // display the cat's name in this iteration of the cat array
    System.out.println ("The new cat's name is " + cat.getcatName() + "!");
}
于 2013-10-18T02:04:02.517 回答
0

您必须将您的猫存储在您创建的数组中。然后你循环并在控制台中显示它们。

Cat[] catArray = new Cat[5];
for (int i=0; i < catArray.length; i ++){
     System.out.println ("Wow! A brand new cat! What's its name?");
     userSays = getLine.next();
     catArray[i] = new Cat(userSays);
}

然后你再次循环:

for (int i=0; i < catArray.length; i ++){
     System.out.println ("The new cat's name is "
             +catArray[i].getcatName() + "!");    
}  

顺便说一句,您应该遵循 java 代码约定变量的名称以小写开头。

于 2013-10-18T01:47:44.747 回答
0
 int [] CatArray;
 Cat[] catName = new String[5]; // Get cat names
 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();

    catName[i] = new Cat(userSays);

 }

 for(int i = 0; i < catName.size; i++) {
      System.out.println ("The new cat's name is "
         catName[i].getcatName() + "!");

 }
于 2013-10-18T01:49:05.157 回答