我对Java很新,而且我正在制作一个小型应用程序,需要两辆车并且返回谁更快。我为这辆车做了另一节课。首先我在main方法中创建了所有对象并且它工作了,然后我尝试将代码放在另一个方法中并从main调用它,只是为了使它更整洁,我得到了一个错误。可能是显而易见的事情,但我太累了,无法直接思考。
错误:(7,14)java:找不到符号 符号:可变法拉利 location:class Test
错误:(7,23)java:找不到符号 符号:变量兰博基尼 location:class Test
public class Test {
public static void main(String[] args) {
createCars();
race(ferrari, lamborghini);
}
public static void createCars() {
Car ferrari = new Car("Ferrari", "California");
ferrari.setHp(552);
ferrari.setAcceleration(3.3);
Car lamborghini = new Car("Lamborghini", "Huracan");
lamborghini.setHp(602);
lamborghini.setAcceleration(2.5);
Car bmw = new Car("BMW", "M5");
bmw.setHp(560);
bmw.setAcceleration(3.7);
Car cadillac = new Car("Cadillac", "CTS-V");
cadillac.setHp(640);
cadillac.setAcceleration(3.6);
}
public static void race(Car carA, Car carB) {
if (carA.getAcceleration() < carB.getAcceleration()) {
System.out.println("The " + carA.getMake() + " " + carA.getModel() + " is faster than the " + carB.getMake() + " " + carB.getModel());
} else if (carB.getAcceleration() < carA.getAcceleration()) {
System.out.println("The " + carB.getMake() + " " + carB.getModel() + " is faster than the " + carA.getMake() + " " + carA.getModel());
} else {
System.out.println("It's a tie");
}
}
}
答案 0 :(得分:4)
您收到该错误,因为您的汽车实例ferrari
和lamborghini
不在主要功能的范围内。
你可以修改你的创建汽车方法,它会返回一个新的汽车insteand:
public static Car createCar(String make, String model, int hp, double acceleration ) {
Car car = new Car(make, model);
car.setHp(hp);
car.setAcceleration(acceleration);
return car;
}
然后你可以使用它:
public static void main(String[] args) {
Car ferrari = createCar("Ferrari", "California", 552, 3.3 );
Car lamborghini = createCar("Lamborghini", "Huracan", 602, 2.5 );
race(ferrari, lamborghini);
}
......或直接
public static void main(String[] args) {
race(
createCar("Ferrari", "California", 552, 3.3 ),
createCar("Lamborghini", "Huracan", 602, 2.5 )
);
}
答案 1 :(得分:3)
提前声明您的对象。这允许变量保留在race()
函数的范围内。否则,只有createCars()
方法会看到您的汽车。
public class Test {
static Car ferrari;
static Car lamborghini;
static Car bmw;
static Car cadillac;
public static void main(String[] args) {
createCars();
race(ferrari, lamborghini);
}
public static void createCars() {
ferrari = new Car("Ferrari", "California");
ferrari.setHp(552);
ferrari.setAcceleration(3.3);
lamborghini = new Car("Lamborghini", "Huracan");
lamborghini.setHp(602);
lamborghini.setAcceleration(2.5);
bmw = new Car("BMW", "M5");
bmw.setHp(560);
bmw.setAcceleration(3.7);
cadillac = new Car("Cadillac", "CTS-V");
cadillac.setHp(640);
cadillac.setAcceleration(3.6);
}
}
答案 2 :(得分:3)
只要创建它们的方法结束,您创建的对象就无法访问(因此可以进行垃圾回收)。这是因为您将它们分配给该方法中的局部变量,并且您的程序的其他任何部分都无法访问这些变量。
您需要一种方法让您的程序的其余部分访问您创建的汽车。一种方法是制作一个&#34;持有&#34;他们,像这样:
class Garage {
private Car ferrari;
private Car lamborghini;
private Car bmw;
private Car cadillac;
// You could use a constructor instead of using a separate method.
public void createCars() {
ferrari = new Car("Ferrari", "California");
ferrari.setHp(552);
ferrari.setAcceleration(3.3);
lamborghini = new Car("Lamborghini", "Huracan");
lamborghini.setHp(602);
lamborghini.setAcceleration(2.5);
bmw = new Car("BMW", "M5");
bmw.setHp(560);
bmw.setAcceleration(3.7);
cadillac = new Car("Cadillac", "CTS-V");
cadillac.setHp(640);
cadillac.setAcceleration(3.6);
}
public void getFerrari() {return ferrari;}
public void getLamborghini() {return lamborghini;}
public void getBmw() {return bmw;}
public void getCadillac() {return cadillac;}
}
现在,您可以在Garage
方法中使用main()
类,如下所示:
public static void main(String[] args) {
Garage garage = new Garage();
// If you move createCars into Garage's constructor, you wouldn't need to call createCars any longer.
garage.createCars();
race(garage.getFerrari(), garage.GetLamborghini());
}