为什么我无法将 appleArray、blueberryArray 和花生数组发送到 calcTotalPies 方法?
final int MAX_PIES = 81;
final int MAX_PER_TYPE = 27;
String typeOfPie = getPieType();
while (!typeOfPie.equalsIgnoreCase("q")) {
if (typeOfPie.equalsIgnoreCase("apple")) {
String[] appleArray = fillApple(typeOfPie, MAX_PER_TYPE);
}
else if (typeOfPie.equalsIgnoreCase("blueberry")) {
String[] blueberryArray = fillBlueberry(typeOfPie, MAX_PER_TYPE);
}
else if (typeOfPie.equalsIgnoreCase("peanut")) {
String[] peanutArray = fillPeanut(typeOfPie, MAX_PER_TYPE);
}
typeOfPie = getPieType();
}
if (typeOfPie.equalsIgnoreCase("q")) {
int totalPies = calcTotalPies(appleArray, blueberryArray, peanutArray);
}
最佳答案
局部变量始终在 block 内声明,并且仅在该 block 中有效(注意:方法体或 if
或循环的主体也是 block )。
您仅在其周围的 if
block 中声明了 appleArray
、blueberryArray
和 peanutArray
,因此它们不是存在于最低的 if
block 中。编译器应该告诉您一些未定义的数组。
关于java - 无法将数组传递到方法中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31349059/