2012-10-01 36 views
1

我認爲我的代碼在這裏有幾個基本問​​題。我對Java語法不太熟悉,所以我不太清楚自己出錯的地方。任何幫助將不勝感激。Java - 語法有我難住

我試圖在中間使用一個構造函數,並向底部使用一些訪問器,但是我認爲我已經爲自己過度複雜了。

- 編輯 -

我已經重新寫了很多的代碼,而且大小下來到我的大問題是哪裏;構造函數。但是,我收到了與提供給我的代碼有關的構造函數的錯誤。

import java.util.Scanner; 

public class VehicleBooking { 

    private String booking_ID = ""; 
    private String registration = ""; 
    private String make_model = ""; 
    private int number_passengers = 1; 
    private boolean insurance_choice = false; 

    public static final int BOOKING_FEE= 100; 
    public static final int EXTRA_PASSENGER = 50; 
    public static final int INSURANCE_FEE = 50; 

    public VehicleBooking(String booking_ID1, String registration1, String make_model1, int number_passengers1) { 

     /** Initialise the variables **/ 
     booking_ID = booking_ID1; 
     registration = registration1; 
     make_model = make_model1; 
     number_passengers = number_passengers1; 
    } 

    public static void main(String args[]) { 
     VehicleBooking vb = new VehicleBooking(booking_ID1, registration1, make_model1, number_passengers1); 
    }  
} 
+0

你不應該」爲同一個流創建多個包裝器,因爲這會導致混淆和錯誤。 'new String(「」)''與''「'很相似,或者根本不設置字符串。 –

+0

有幾個問題。其中之一:Java中的比較使用'==',而不是'=':if(insurance_flag = false)'=>'if(insurance_flag == false)'。這又可以寫成:'if(!insurance_flag)'。 – assylias

+0

您是否收到錯誤?首先我可以看到,在getBookingFee中,你需要兩個=而不是一個=。此刻,您正在將insurance_choice分配給true,而不是檢查相等性 – RNJ

回答

6

先不要初始化類似下面的字符串: -

private String booking_ID = new String(""); 

而是使用String.valueOf()或只是分配一個空字符串變量: -

private String booking_ID = ""; 

其次,做一個習慣用UPPER_CASE聲明CONSTANTS字母: -

public static final int BOOKING_FEE = 100; 

三,看看你的構造: -

VehicleBooking() { 
     Scanner input = new Scanner(System.in); 
     Scanner scan = new Scanner(System.in); 

     System.out.print("Enter booking ID"); 
     booking_ID = input.next(); 
     System.out.print("Enter registration number"); 
     registration = input.next(); 
     System.out.print("Enter vehicle make/model"); 
     make_model = input.next(); 
     System.out.print("Enter number of passengers"); 
     number_passengers = scan.nextInt(); 
    } 

你不應該做在你的構造I/O操作.. Constructors用於初始化對象的狀態。它的唯一目的是initialize

對於I/O的目的,使不同的方法readInput()並在您的對象創建後調用它。

另一件事: -這裏是你用你的if-else-if塊..

  if (insurance_flag = false) { 
      insurance_flag = true; 
      return true; 
     } else if (insurance_flag = true) { 
      return false; 
     } 

在這段代碼中,你if總是false和你else if總是true ..因爲你實際上是在分配這些values您的insurance_flag ..您應該使用==進行比較。

因此,請使用if (insurance_flag == false)。其實你不需要用一個布爾值進行比較......

只需使用: - if (!insurance_flag) ..他們是等價的..

理想情況下,你應該改變包含您的方法如果上面到下面的一個: -

 public boolean addInsurance(){ 
      boolean returnValue = !insurance_flag; 
      insurance_flag = true; 
      return returnValue; 
     } 

因爲這就是你的方法做,但在一種奇怪的方式..

您也可以替換final_cost = final_cost + insurance_fee;與下面的代碼: -

final_cost += insurance_fee; 

通過使用這種方式,final_cost不會被評估twice ..

編輯**: -

constructor應看起來像這樣: -

public VehicleBooking(String bookingId, String registration, String makeModel, 
         String numberOfPassengers) { 

    /** Initialize the instance variables **/ 
    /** this represent the reference to current object **/ 
    this.booking_ID = bookingId; 
    this.registration = registration; 
    this.make_model = makeModel; 
    this.number_passengers = numberOfPassengers; 
} 

所以,你實際上是通過這些你讀值,到構造函數中的參數,並初始化您的實例屬性,這些參數..

我認爲這將清除您的疑問..

+0

+1,但你應該**從來沒有**無論如何與布爾文字進行比較。只需使用'if(condition)'或'if(!condition)'。 –

+0

@KonradRudolph ..噢...將編輯 –

+0

這並不完全是什麼'如果(insurance_flag)'做 - 如果它已經被設置爲TRUE;它保持這樣。也許將其重寫爲'public boolean addInsurance(){boolean ret =!insurance_flag; insurance_flag = true;返回ret; }' – Edd

0

首先,不使用

private String booking_ID = new String(""); 

你正在創建一個新的空不變。您至少應該讓JVM實習生爲空字符串!

if (insurance_flag = false) 

這是分配,不評價,使用==,更好的仍然只是如果(!insurance_flag)

public boolean addInsurance(){ 
     insurance_choice = true; 
     if (insurance_flag = false) { 
      insurance_flag = true; 
      return true; 
     } else if (insurance_flag = true) { 
      return false; 
     } 
     return true; 
    } 

這整個功能不使一個很大的意義,你的編碼在圈子。只需將保險標誌設置爲true即可。除了退貨之外,Insurance_flag不會用於任何地方...爲什麼只想在標誌的第一個設置上返回true?只要使其無效。爲「設置」行動。另外,你正在將製作和模型合併爲一個字符串,而不是真正的OO構建項目的方式。理想情況下,您應該將Vehicle對象附加到您的VehicleBooking上,而不是Vehicle本身的屬性。

溝內部類,不要在構造函數中執行輸入。從輸入構建(即傳入)或使用構建器。