2014-07-01 48 views
0

我想要創建一個程序,我要求用戶輸入三個人的三個細節,然後再顯示給用戶。如何使用Java中的對象數組接收信息?

這是我想出的解決方案,它似乎沒有工作。

import java.util.Scanner; 

class Human { 
    int age; 
    String name; 
    String gender; 
} 

public class Database { 

    public static void main(String[] args) {  
     System.out.println("Please enter the information of ten people one by one."); 

     Scanner input = new Scanner(System.in); 

     Human[] person = new Human[3]; 

     for(int i =0; i < person.length; i++){ 
      person[i].age = input.nextInt(); 
      person[i].name = input.nextLine(); 
      person[i].gender = input.nextLine();   
     } 


     System.out.println("Here are the people that you entered: "); 

     for(int i = 0; i <person.length; i++){ 
      System.out.println(person[i].age); 
      System.out.println(person[i].name); 
      System.out.println(person[i].gender); 
     } 

     input.close(); 

    } 
} 
+3

什麼是不工作:例如,在第一個循環,你可以在它的開頭寫的嗎?相反,它是否告訴你錯誤在哪裏?或者你有什麼投入?等等。 – jhobbie

+0

你能解釋一下你期望的行爲和你實際得到的行爲,以及這個問題到底是什麼? –

+0

問題已解決。下面的解決方案是我打算讓我的原始程序去做的。 –

回答

1
public static void main(String[] args) throws Exception { 

    Scanner input = new Scanner(System.in); 
    System.out.print("How many people are you entering? "); 
    int people = input.nextInt(); 
    Human[] person = new Human[people]; 

    for (int i = 0; i < people; i++) { 

     System.out.println("Creating new human number : " + i); 
     Human h = new Human(); 
     person[i] = h; 

     System.out.print("Please enter an age: "); 
     person[i].age = input.nextInt(); 
     System.out.print("Please enter a name: "); 
     person[i].name = input.next(); 
     System.out.print("Please enter a gender: "); 
     person[i].gender = input.next(); 

    } 

    System.out.println("Here are the people that you entered: "); 

    for (int i = 0; i < person.length; i++) { 
     System.out.print(person[i].age + " "); 
     System.out.print(person[i].name + " "); 
     System.out.println(person[i].gender); 
    } 

    input.close(); 

} 

你永遠不會實例化一個新的Human對象,並把它放入數組,所以當你person[i]你正在訪問一個空指針。在上面的代碼中,我實例與

Human h = new Human(); 

對象,然後把它放入陣列

person[i] = h; 

這將固定空指針。我也修正了閱讀邏輯,所以它會循環多次,每次都要求輸入信息。

+0

非常感謝! 你能簡要解釋一下「拋出異常」是什麼嗎? –

+0

對此遺憾,這是遺漏的代碼。我使用相同的文件來編寫測試,以找到關於SO的問題的答案,所以不是一堆異常處理,而是拋出任何異常。該代碼部分可以被刪除。當你在一個函數中添加'throws [throwable type]'時,它允許在該函數中發生的任何異常被拋出一個級別(在函數外)並在別處被捕獲和處理。 –

0

以下應該工作得很好。

for(int i = 0; i <person.length; i++){ 
     System.out.println(person[i]); 
    } 

您正在使用循環,所以您確實不需要像以前那樣從數組中提取每個屬性。如果你正在迭代數組,那麼for循環很好,否則就擺脫循環。

2
public static void main(String[] args) { 
    ArrayList<Human> list = new ArrayList<>(); 
    Scanner in = new Scanner(System.in); 

    for(int i = 0; i < 3; i++) { 
     list.add(new Human()); 
     System.out.print("Name: "); list.get(i).name = in.next(); 
     System.out.print("Age: "); list.get(i).age = Integer.parseInt(in.next()); 
     System.out.print("Gender: "); list.get(i).gender = in.next(); 
    } 
} 

在這種方式它應該工作。

+0

這段代碼使用了我並不熟悉的東西。隨着我學習新的東西,我會在稍後嘗試。 –

+0

哪些事情你不熟悉? – Remix

+0

list.get whatever,arraylist和in.next()。 我正在從Udemy開始初級課程,所以我應該在幾天內學習它。 –

0

實例化對象的人,使其工作

for(int i =0; i < person.length; i++){ 
     person[i] = new Human(); 
     person[i].age = input.nextInt(); 
     person[i].name = input.nextLine(); 
     person[i].gender = input.nextLine();   
    } 
0

Human[] person = new Human[3];

只分配空間持有引用三個Human對象。它不會創建任何Human對象。最初,陣列中的條目將爲null。您仍然需要創建每個Human

person[i] = new Human();

相關問題