2017-04-02 62 views
-3

我想從同一個src文件夾中調用類「CarFuel」。它不工作並顯示錯誤。在同一個src文件夾中調用不同的類將不起作用

規範類的主要方法:

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) ;是不打電話給其他班,並輸出一個錯誤?

+1

其一,'car.GetGasoline'缺少括號:'car.GetGasoline()' –

+0

對於另一個 - 是你看到了什麼錯誤? –

+0

對於二者,這不是C#,所以如果您希望讓Java人更容易讀取您的代碼,請不要大寫方法 –

回答

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 
    } 
} 

試試這個代碼,它的工作.....

+0

@tobloef plz確認你已編輯?我看不到任何變化 –

+0

我修正了代碼塊的格式,第一行和最後一行沒有被格式化。您可以點擊「編輯X小時前」查看更改。 – tobloef

相關問題