2013-10-18 30 views
2

我的代码看起来像这样至今:如何在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个名称输入后输出它们?

+0

澄清一下,我该怎么办记得循环结束后所有小猫的名字? –

回答

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() + "!"); 
} 
+0

我在哪里把我? –

+0

@ K-Java-K不要简单地复制答案。通过它。 – gjman2

0

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

Cat[] catArray = mew 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代码约定变量名以小写。

+0

我刚开始学习java,而我的老师并没有完全解释它。当我输入你的代码时,它给了我一个在CatArray [i]上的错误:required:int found:Cat –

+0

使用String [] CatArray = new String [5];不是INT – gjman2

+0

@ K-Java-K对不起,我更新了答案,你必须定义你的数组像'猫[]' – nachokk

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() + "!"); 

} 
1

你需要存储的Cat小号莫名其妙。

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. 
}