【发布时间】:2021-08-11 12:57:50
【问题描述】:
我已经创建了一个对象数组,它是从一个名为 dataInput() 的方法创建的。我想在 main 方法中调用 people 数组。提前致谢。
public static void dataInput() {
Scanner input = new Scanner(System.in);
int noOfPersons = 3;
String name = "";
int no = 0;
String city = "";
inputTry[] persons = new inputTry[3];
persons[0] = new inputTry(name, no, city);
persons[1] = new inputTry(name, no, city);
persons[2] = new inputTry(name, no, city);
for (int i = 0; i < noOfPersons; i++) {
System.out.print("Enter person name: ");
name = input.nextLine();
System.out.print("Enter person number: ");
no = input.nextInt();
input.nextLine();
System.out.print("Enter where the person lives: ");
city = input.nextLine();
persons[i].name = name;
persons[i].no = no;
persons[i].city = city;
}
}
【问题讨论】:
-
可以让
dataInput返回数组,这样就可以在调用方法中访问了。 -
注意 java 命名约定。变量名应以小写字母开头,类名应以大写字母开头。