2013-04-16 52 views
-1
import java.util.Scanner; 
public class Bankaccount{ 
    double diposit, withdraw; 
double balance=0; 
public Bankaccount(double balance) 
{ 
    this.balance=balance; 
} 
    void deposit(double ammount) 
{ 
     Scanner b=new Scanner(System.in); 
    System.out.println("diposit a amount"); 
    ammount=b.nextInt(); 
    balance +=ammount; 
} 
void withdraw(double ammount) 
{ 
     Scanner b=new Scanner(System.in); 
    System.out.println("withdraw a amount"); 
    ammount=b.nextInt(); 
    balance +=ammount; 
} 

    void display() 
    { 
     System.out.println(balance); 
    } 
public static void main(String[]args) 
{ 


}} 

如何在主函數中顯示這些條件?由於餘額不足,我已經創建了一個例外。我想在這裏拋出異常。在充足的餘額異常

+0

你可以重新構建你的問題?最新的實際問題是什麼? –

+0

我沒有看到任何條件嗎? –

回答

1

用這個來拋出異常。

throw new InsufficientBalanceException(); 

,並宣佈該方法

void withdraw (double amount) throws InsufficientBalanceException 
{ 
    if (amount > balance) 
     throw new InsufficientBalanceException(); 
    else 
     balance -= amount; 
} 

希望這有助於。

0

首先,當你退出,你必須減去的量不會增加,但首先你必須檢查你是否可以減去這一數額:

void withdraw(double ammount) throws InsuffcientBalanceExeption 
{ 
    Scanner b=new Scanner(System.in); 
    System.out.println("withdraw a amount"); 
    ammount=b.nextInt(); 
    if(balance<amount){ 
     throw new InsuffcientBalanceExeption(); 
    } 
    balance -=ammount; 
} 
+1

也添加拋出子句。 – Algorithmist

+0

是啊,好趕:) – Stephan