2014-09-10 57 views
0

我正在創建一個提示輸入姓氏,年齡和體重的程序。然後顯示該信息,然後再次詢問。鍵入「完成」將使其停止詢問,並將顯示每個人的所有信息。在Loop中創建對象的新實例

類「Person」的姓氏爲字符串,年齡和體重爲雙倍。

我一直在如何在for循環中創建一個人的新實例,然後在循環完成後打印出每個實例。

public static void main(String[] args) { 
    Scanner userInput = new Scanner(System.in); 
    String decision; 
    Person Guy = null; 

    for (int i =0; i<20; i++){ 
    Guy = new Person(); 
     System.out.println("Please enter a last name");  
     Guy.lastName = userInput.next(); 
      if (Guy.lastName.equalsIgnoreCase("finished")){ 
     break; 
     } 
    System.out.println("Please enter an age"); 
     Guy.age = userInput.nextDouble(); 
    System.out.println("Please enter a weight"); 
     Guy.weight = userInput.nextDouble(); 
    Guy.WriteOutput();  
    } 

    for (int i=0; i<20; i++){ 
     Guy.WriteOutput(); 
     if (Guy == null){ 
      break; 
     } 
    } 
} 

}

回答

0

你滴上每次迭代地板的老傢伙。相反,您希望將每個人添加到一個結構中(例如ArrayList),並在輸入階段完成後,遍歷該列表並打印出每個人。