2013-12-14 57 views
1

我使用eclipse查看我的代碼,出現的最常見錯誤是「令牌上的語法錯誤,錯誤的構造(s)」我不確定我在做什麼錯了,但我對Java相當陌生。Java編程的銀行帳戶代碼

我的代碼應該從銀行賬戶中提取一個指定的(用戶輸入)金額,我從$ 10,000開始並設置程序,以便如果提款金額小於0或大於$ 10,000,它將觸發一個斷言錯誤。

class ThreadsUnitProject2 { 
public static void main(Sting args []) 
// Field member 
private int balance; 

public void BankAccount() 
{ 
balance = 10000; 
} 

public int withdraw(int amount) 
{ 
// Subtract requested amount from balance 
balance-=amount; 

// Return requested amount 
return amount; 
} 


public int getBalance() 
{ 
return balance; 
} 


import java.util.Scanner; 

class BankAccountTester extends BankAccount 
{ 
public static void main(String[] args) 
{ 
    Scanner scan = new Scanner(System.in); 

    BankAccount myAccount = new BankAccount(); 

    System.out.println("Balance = " + myAccount.getBalance()); 

    System.out.print("Enter amount to be withdrawn: "); 
int amount = scan.nextInt(); 

    assert (amount >= 0 && amount <= myAccount.getBalance()):"You can't withdraw that amount!"; 

    myAccount.withdraw(amount); 

    System.out.println("Balance = " + myAccount.getBalance()); 
} 

謝謝你的幫助!

+1

將您的'import java.util.Scanner;'移動到文件的頂部。 –

+0

'public static void main(Sting args [])'這需要括號。其他的東西。 –

+0

您應該在提交時修復縮進,以便人們更容易閱讀。另外,你似乎有兩個'main'方法? – AVP

回答

0

類「ThreadsUnitProject2」重命名的BankAccount,並從該刪除main()方法,使BankAccountTester類公共終於從的BankAccount構造

0

刪除無效在代碼中看到這麼多的問題後,我決定,我應該只是修復它讓你嘗試從下面的解決方案中學習。

這應該是第一個Class文件。

public class BankAccount { 

    private int balance; 

    public BankAccount() {  //constructor 
     balance = 10000; 
    } 

    public int withdraw(int amount) { 
     balance -= amount; 
     return amount; 
    } 

    public int getBalance() { 
     return balance; 
    } 
} 

這應該是您的第二個Class文件。這將包含您的main方法,它將測試您的BankAccount類。

import java.util.Scanner; 

public class BankAccountTester { 
    public static void main(String[] args) { 
     Scanner  scan  = new Scanner(System.in); 
     BankAccount myAccount = new BankAccount(); 

     System.out.println("Balance = " + myAccount.getBalance()); 
     System.out.print ("Enter amount to be withdrawn: "); 
     int amount = scan.nextInt(); 

     assert (amount >= 0 && amount <= myAccount.getBalance()) : "You can't withdraw that amount!"; 
     myAccount.withdraw(amount); 

     System.out.println("NewBalance = " + myAccount.getBalance()); 
    } 
} 

請閱讀並複習this link to proper coding conventions

+0

此外,assert不應該用於程序邏輯,因爲部署時默認情況下它們被禁用,需要使用-ea參數啓用。很好的解釋在這裏:http://stackoverflow.com/questions/2758224/assertion-in-java – LINEMAN78