2013-10-18 27 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. 
}