gpt4 book ai didi

java - 对象数组困惑

转载 作者:行者123 更新时间:2023-12-01 13:18:20 25 4
gpt4 key购买 nike

程序开始时会提示:

  1. 创建新的学生列表
  2. 搜索学生
  3. 退出

问题:在第二个 if 语句中 oClassList 未初始化。如何在第一个 if 语句中实例化的对象数组中搜索第二个 if 语句中的姓氏?

    int z = 0;

while(run) {

if(z == 0) {
System.out.println("Please choose an option. (Enter 1, 2, or 3)\n");
}
else {
System.out.println();
System.out.println("Would you like to continue? If so, please choose an option.\n");
}

System.out.println("1. New Class List");
System.out.println("2. Search for a Student");
System.out.println("3. Exit");

iUserSelection = oScan.nextInt();


Student[] oClassList;

// creates new class list
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();

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(iUserSelection == 3) {
run = false;

System.out.println();
System.out.println("Goodbye.");
}





z++;
}

最佳答案

您的变量 Student[] oClassList; 是一个局部变量,嵌套在 while 循环内。这将导致每次程序离开 while 范围时重置 oClassList(因此每个 while 处理都会有自己的类列表)。

您只需将该变量声明移至 while 循环上方 - 该变量的范围将跨越整个程序。

因此,只需将 Student[] oClassList; 直接移到 int z = 0; 下面,一切都会正常工作。

<小时/>

编辑:

作用域并不是一切,你还必须初始化变量。空检查(如另一个答案中提到的)也不是一个坏主意。

int z = 0;

Student[] oClassList = null;


while(run) {

if(z == 0) {
System.out.println("Please choose an option. (Enter 1, 2, or 3)\n");
}
else {
System.out.println();
System.out.println("Would you like to continue? If so, please choose an option.\n");
}

System.out.println("1. New Class List");
System.out.println("2. Search for a Student");
System.out.println("3. Exit");

iUserSelection = oScan.nextInt();


// creates new class list
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();

System.out.println("Student search");

if (oClassList == null) {
System.out.println("No class list defined yet");
} else {

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(iUserSelection == 3) {
run = false;

System.out.println();
System.out.println("Goodbye.");
}





z++;
}

关于java - 对象数组困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22275908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com