我想要創建一個程序,我要求用戶輸入三個人的三個細節,然後再顯示給用戶。如何使用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();
}
}
什麼是不工作:例如,在第一個循環,你可以在它的開頭寫的嗎?相反,它是否告訴你錯誤在哪裏?或者你有什麼投入?等等。 – jhobbie
你能解釋一下你期望的行爲和你實際得到的行爲,以及這個問題到底是什麼? –
問題已解決。下面的解決方案是我打算讓我的原始程序去做的。 –