这是我的代码。当我试图将对象for
放入汽车数组时,问题出现在newCar
循环中。它说Car[]
无法解析为变量,即使我在第四行代码中声明它。
public class Race {
public double distance;
public String raceType;
public Car[] carsEntered = new Car[3];
final int DEFAULT_NUMBER_OF_CARS = 3;
public static void main(String[] args) {
int carCount = 0;
String winner;
Car myCar = new Car("Chase", 75);
Car ProfCar = new Car("Prof. Harms", 85);
Car JeffCar = new Car("Jeff Gordan", 100);
}
public void changeDistace(double newDistance) {
distance = newDistance;
}
public void changeRaceType(String newRaceType) {
raceType = newRaceType;
}
public void addCar(Car newCar) {
boolean carPlaced = false;
for (int i=0; i < DEFAULT_NUMBER_OF_CARS; i++) {
if (carPlaced == false) {
if (Car[i] == null) {
Car[i] = newCar;
carPlaced = true;
}
}
}
}
}
注意:这不是项目中唯一的.java文件。汽车对象被声明并在我的其他代码中工作正常,我只需要知道为什么我声明的数组(Car[]
)在for
循环中未被识别{{1} }}
答案 0 :(得分:3)
Car[]
是变量的类型,而不是名称。该变量名为carsEntered
。