我试图从同一个src文件夹中调用该类,#34; CarFuel"。它不起作用并显示错误。
使用main方法的类代码:
public class CarFuel {
int CarFuel = 5; //Gallons of fuel starting in car
public CarFuel(int FuelAmount){ //Constructor for defined staring fuel amount
CarFuel = FuelAmount;
}
public void GetGasoline(int amount){ //Filling up gasoline, amount = how much gasoline filled up
CarFuel = CarFuel + amount;
}
public void DrainGasoline(int amount){ //Driving car, amount = how much gasoline drained
CarFuel = CarFuel - amount;
}
public int GetGasoline(){ //returns how much gasoline is left in the end
return CarFuel;
}
}
//MAIN EXEX IN CarFuelExec
以下是使用这些方法的主要类:
public class CarFuelExec {
public static void main(String[] args){
CarFuel.car = new CarFuel(0);
car.GetGasoline(36);
car.DrainGasoline(5);
System.out.println("The remaining fuel in your car is: " + car.GetGasoline + " gallons");
}
}
有人可以告诉我为什么CarFuel.car =新Carfuel(0);是不是正在调用另一个类并输出错误?
答案 0 :(得分:0)
public class CarFuelExec {
public static void main(String[] args){
CarFuel car = new CarFuel(0);//make object of Car_Fuel
car.GetGasoline(36);
car.DrainGasoline(5);
System.out.println("The remaining fuel in your car is: " + car.GetGasoline() + " gallons"); //method GetGasoline need parenparenthesis
}
}
试试这个代码它会起作用.....