2013-05-08 93 views
0
public class Account 
{ 
static int accountNum; 
static int nextAccountNum = 112200; 
static double balance; 


    public static void main(String args[]) 
    { 
     Account checkingAccount = new Account(1500.00); 
     Account savingAccount = new Account(1000.00); 
     Account moneyMarketAccount = new Account(500.00); 

     checkingAccount.getAccountNumber(); 
     savingAccount.getAccountNumber(); 
     moneyMarketAccount.getAccountNumber(); 

    } 

    public Account(double bal) 
    { 
     balance = bal; 
     accountNum = nextAccountNum; 
     nextAccountNum++; 
    } 

} 

每次運行它時,我都不會收到任何返回變量。我看到的只是過程完成的文本。尋找幫助無法返回變量java

+1

什麼是你期待得到什麼?打印輸出? – Keppil 2013-05-08 22:14:29

回答

1

你沒有輸出任何東西。使用System.out.println輸出帳戶餘額,例如

System.out.println(checkingAccount.getAccountNumber()); 

此外,請您accountNum和你balance變量非static,否則餘額和賬戶號碼將被覆蓋,每一個新的實例。

0

一個問題是accountNumbalance被宣佈爲static。您需要它們作爲實例字段,因此Account的每個實例都有其自己的值。

你得不到輸出的原因是你的main()方法創建了幾個Account對象,但對它們沒有任何作用。

public class Account 
{ 
    static int nextAccountNum = 112200; 
    int accountNum; 
    double balance; 

    public static void main(String args[]) 
    { 
     Account checkingAccount = new Account(1500.00); 
     Account savingAccount = new Account(1000.00); 
     Account moneyMarketAccount = new Account(500.00); 

     checkingAccount.getAccountNumber(); 
     savingAccount.getAccountNumber(); 
     moneyMarketAccount.getAccountNumber(); 

     System.out.println("Checking: " + checkingAccount.toString());  
     System.out.println("Savings: " + savingAccount.toString());  
     System.out.println("Money market: " + moneyMarketAccount.toString());  
    } 

    public Account(double bal) 
    { 
     balance = bal; 
     accountNum = nextAccountNum; 
     nextAccountNum++; 
    } 

    @Override 
    public String toString() { 
     return "Account " + accountNum + ": balance=" + balance; 
    }  
} 
1

將主函數與具有多個實例的類分開通常是很好的做法。 Getters和setters方法用於正確封裝類變量,因爲帳戶可能應該有。下面是一個例子。

Main.java

public class Main { 
    public static void main(String args[]) 
    { 
     Account checkingAccount = new Account(1,1500.00); 
     Account savingAccount = new Account(2,1000.00); 
     Account moneyMarketAccount = new Account(3,500.00); 

     System.out.println("Account " + checkingAccount.getAccountNumber() 
       + " has balance of: " + checkingAccount.getBalance()); 
     System.out.println("Account " + savingAccount.getAccountNumber() 
       + " has balance of: " + savingAccount.getBalance()); 
     System.out.println("Account " + moneyMarketAccount.getAccountNumber() 
       + " has balance of: " + moneyMarketAccount.getBalance()); 
    } 
} 

Account.java

public class Account 
{ 
private int accountNum; 
private int nextAccountNum = 112200; 
private double balance; 

    public Account(int accountNum, double balance) 
    { 
     this.balance = balance; 
     this.accountNum = accountNum; 
    } 

    public int getAccountNumber() { 
     return accountNum; 
    } 

    public double getBalance() { 
     return balance; 
    } 

    public void setBalance(int balance) { 
     // check if balance variable is a correct value 
     // before setting it to the class variable 
     this.balance = balance; 
    } 

}