请帮我告诉我为什么第二个循环' row'在printSeating方法中找不到。
public void printSeating()
{
for (int row = FIRST_ROW_NUMBER; row <= firstClass.length; row++)
{
if (row < 10)
System.out.print(" ");
System.out.print(row);
System.out.println(":" + firstClass[row - FIRST_ROW_NUMBER]);
}
int firstEconomyRowNumber = FIRST_ROW_NUMBER + firstClassRowCount;
int lastEconomyRowNumber = firstEconomyRowNumber + economyRowCount - 1;
for (int row = firstEconomyRowNumber;
row <= lastEconomyRowNumber; row++);
{
if (row < 10)
System.out.print(" ");
System.out.print(row);
System.out.println(":" + economy[row - firstEconomyRowNumber]);
}
}
答案 0 :(得分:2)
当声明FOR循环时,在循环之后放置一个分号,如下所示:
for (int row = firstEconomyRowNumber;
row <= lastEconomyRowNumber; row++);{
//..insert code here
}
这声明了一个没有正文的FOR循环,所以当你在这个语句之后访问变量行时,它找不到变量,因为它只能用在FOR循环的不存在的主体中。要解决此问题,您需要删除分号,如下所示:
for (int row = firstEconomyRowNumber;
row <= lastEconomyRowNumber; row++){
//...insert code here
}
答案 1 :(得分:1)
这是你的错误。
for (int row = firstEconomyRowNumber;
row <= lastEconomyRowNumber; row++);
在for循环之后摆脱;
,它应该有效。