程序开始时会提示:
- 创建新的学生列表
- 搜索学生。
问题:我创建一个对象数组并将其填充到第一个 if 语句中,然后尝试在第二个 if 语句中访问它,但我知道我不能这样做。那么如何创建和填充对象数组并稍后访问它呢?有什么想法吗?
if(iUserSelection == 1) {
System.out.println();
System.out.println("How many students?");
x = oScan.nextInt();
System.out.println();
// flush the buffer
oScan.nextLine();
Student[] oClassList = new Student[x];
for(int i = 0; i < x; i++) {
System.out.println("*********************");
System.out.println("Student " + (i + 1) + " of " + x);
System.out.println("*********************");
oClassList[i] = new Student("","",0,0,0,0);
System.out.print("First Name: ");
oClassList[i].setFirstName(oScan.nextLine());
System.out.print("Last Name: ");
oClassList[i].setLastName(oScan.nextLine());
System.out.print("Homework average: ");
oClassList[i].setHWAve(oScan.nextInt());
System.out.print("Quiz average: ");
oClassList[i].setQuizAve(oScan.nextInt());
System.out.print("Project average: ");
oClassList[i].setProjectAve(oScan.nextInt());
System.out.print("Test average: ");
oClassList[i].setTestAve(oScan.nextInt());
// flush the buffer
oScan.nextLine();
System.out.println();
oClassList[i].printStudent();
}
}
if(iUserSelection == 2) {
// flush the buffer
oScan.nextLine();
if(oClassList[0] != null) {
System.out.println("Student search");
System.out.print("Enter last name: ");
sSearchLastName = oScan.nextLine();
System.out.print("Enter first name: ");
sSearchFirstName = oScan.nextLine();
}
for(int y = 0; y >= oClassList.length; y++) {
if(sSearchLastName == oClassList[y].lastName) {
System.out.println("found elements");
}
else
System.out.println("Error - Student not found");
}
}
最佳答案
要防止数组在 if 语句退出时被删除,请在 if 语句之外声明它,从而为其提供更广泛的范围。然后,可以在 if 语句内部填充该数组,而不会在 if 语句退出时超出范围。例如,
int[] arr;
if (true) {
arr = new int[1];
arr[0] = 5;
}
System.out.println(arr[0]);
输出:
5
arr 在退出 if 语句时将保持其值,因为它是在 if 语句外部声明并在 if 语句内部实例化的。
您更正后的代码将是:
Student[] oClassList; //Declared outside of both if-statements
if(iUserSelection == 1) {
System.out.println();
System.out.println("How many students?");
x = oScan.nextInt();
System.out.println();
// flush the buffer
oScan.nextLine();
oClassList = new Student[x];
for(int i = 0; i < x; i++) {
System.out.println("*********************");
System.out.println("Student " + (i + 1) + " of " + x);
System.out.println("*********************");
oClassList[i] = new Student("","",0,0,0,0);
System.out.print("First Name: ");
oClassList[i].setFirstName(oScan.nextLine());
System.out.print("Last Name: ");
oClassList[i].setLastName(oScan.nextLine());
System.out.print("Homework average: ");
oClassList[i].setHWAve(oScan.nextInt());
System.out.print("Quiz average: ");
oClassList[i].setQuizAve(oScan.nextInt());
System.out.print("Project average: ");
oClassList[i].setProjectAve(oScan.nextInt());
System.out.print("Test average: ");
oClassList[i].setTestAve(oScan.nextInt());
// flush the buffer
oScan.nextLine();
System.out.println();
oClassList[i].printStudent();
}
}
if(iUserSelection == 2) {
// flush the buffer
oScan.nextLine();
if(oClassList[0] != null) {
System.out.println("Student search");
System.out.print("Enter last name: ");
sSearchLastName = oScan.nextLine();
System.out.print("Enter first name: ");
sSearchFirstName = oScan.nextLine();
}
for(int y = 0; y >= oClassList.length; y++) {
if(sSearchLastName == oClassList[y].lastName) {
System.out.println("found elements");
}
else
System.out.println("Error - Student not found");
}
}
关于java - if 语句中创建的对象数组(范围问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22274480/