2016-03-04 94 views
-1

我目前正在製作一個程序來存儲拖拉機租賃數據的過程中,我遇到了我的拖拉機類中的問題,我需要重寫toString方法來打印出來所有的拖拉機的信息,還有一種方法來計算出租利潤,這只是租賃天* rentalrate,但我得到這些錯誤,我不知道從哪裏去這裏。遇到麻煩創建幾種方法

public int RentalProfit(int RentalRate, int RentalDays) 
    { 
     RentalProfit = RentalRate * RentalDays; 
     return this.RentalProfit; 
    } 


    @Override 
    public String toString() { 
     return "Tractor (Rental days = " + RentalDays + ", Rental Rate = " + RentalRate + 
     "Rental profit = " RentalProfit + ", VehicleID = " + VehicleID ")"; 
    } 

繼承人的錯誤:

Tractor.java:59: error: ';' expected 
     "Rental profit = " RentalProfit + ", VehicleID = " + VehicleID ")"; 
         ^
Tractor.java:59: error: not a statement 
     "Rental profit = " RentalProfit + ", VehicleID = " + VehicleID ")"; 
                 ^
Tractor.java:59: error: ';' expected 
     "Rental profit = " RentalProfit + ", VehicleID = " + VehicleID ")"; 
                    ^
3 errors 



public static void main(String[] args){ 
      Tractor tractor; 
      tractor = new Tractor(); 
      tractor.setRentalRate(9); 
      tractor.setRentalDays(45); 
      tractor.setVehicleID(9145949); 
      toString(); 

繼承人的錯誤:

testTractor.java:11: error: non-static method toString() cannot be referenced from a static context 
      toString(); 
     ^
1 error 
+1

兩個缺少'+':'「租金盈利=」+ RentalProfit +「,」VehicleID =「+ VehicleID +」)「;' – Spotted

+0

在你的主:'toString()'應該是'tractor.toString()'; – AmazingDreams

回答

1

你缺少一個+標誌:

return "Tractor (Rental days = " + RentalDays + ", Rental Rate = " + RentalRate + 
    "Rental profit = " + RentalProfit + ", VehicleID = " + VehicleID + ")"; 
        ^           ^
        here          and here 
+0

即使是2,如果你專心看。 :-) – Spotted

+0

明白了!謝謝! :D –

+0

另一個問題。所以我是我的主要拖拉機班,我試圖把這個toString()放到一個拖拉機對象中,我應該怎麼做? –

0

在你的主要方法,你在呼喚toString();你不能這麼做 - toString不是一個靜態方法,必須通過實例調用。你想要的是返回字符串tractor.toString();

+0

是的,我想現在它給我這個產量,拖拉機(租賃天數= 9145949,租金率= 0,租金利潤= 0,VehicleID = 9145949) 我不知道爲什麼它這樣做。 –

0

第一個錯誤,你必須在第二個問題關閉它這樣

return ("Tractor Rental days = " + RentalDays + " Rental Rate = " + RentalRate + 
     "Rental profit = "+ RentalProfit + ", VehicleID = " + VehicleID); 

我認爲你有主要方法,它是靜態的,你需要做靜態方法來調用它使方法toString()靜態... 我希望我幫你