2017-07-18 160 views
-1

只需1節課。我有一個錯誤。當鼠標懸停的錯誤,它提示了「令牌語法錯誤」;「,=預計」 以下是我的編碼,請幫助我!非常感謝朋友。 enter image description hereJava錯誤 - 令牌上的語法錯誤「;」,=,預期

package HelloWorld; 

public class BankAccountManagement { 
    private String accountNumber; 
    private double balance; 
    private String customerName; 
    private String email; 
    //private String phoneNumber; 

    this.balance=100.00; 

    public void withdrawn(double amount) { 
     if (this.balance < amount) { 
      System.out.println("không thể rút dược" + amount); 
      System.out.println("chỉ có thể rút dược" + this.balance); 
     } else { 
      // public void deposit(double amount){ 
      this.balance += amount; 
      System.out.println("bạn gửi thành công" + amount); 
      System.out.println("so du hien tại" + this.balance); 
      //System.out.println("phone" + this.phoneNumber); 

     } 
    } 
} 

回答

0
this.balance=100.00; 

這行自由浮動的Java不支持。將該行代碼移動到構造函數或塊或方法。

看起來像這是默認值,我強烈建議有一個構造函數,並在那裏移動那一行。

public BankAccountManagement() { 
    this.balance=100.00; 
} 
+0

或者直接在聲明中進行初始化。 – talex

+0

[Here](https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html)是_this_的Java tuturial –

1

你行

this.balance=100.00; 

是Java在正確的上下文中的有效行,但不只是對自己。它需要在一個方法裏面。

實現你想在這裏做什麼最簡單的方法是,當你聲明變量初始化這個值:

private double balance=100.00; 

然後,您可以刪除所賜你的問題就行了。

您收到的錯誤信息有點令人困惑,誠然。編譯器總是很難知道你在做什麼。它也很難簡潔地告訴你它期望的是什麼,因爲在代碼中你可能已經寫了很多可能的語法正確的東西。

相關問題