2013-03-20 62 views
2

我正在使用程序類來嘗試測試我的對象類中的方法以查看它們是否工作。這是一個燃氣表讀數系統,我試圖存入資金來償還客戶欠款的餘額。爲什麼我的方法沒有返回字符串?

我的對象類組成:

package GasAccountPracticeOne; 

public class GasAccountPracticeOne 

{ 
    private int intAccRefNo; 
    private String strName; 
    private String strAddress; 
    private double dblBalance = 0; 
    private double dblUnits; 
    private double dblUnitCost = 0.02; 

    public GasAccountPracticeOne(int intNewAccRefNo, String strNewName, String strNewAddress, double dblNewUnits) 
    { 
     intAccRefNo = intNewAccRefNo; 
     strName = strNewName; 
     strAddress = strNewAddress; 
     dblUnits = dblNewUnits; 

    }//end of constructor 

    public GasAccountPracticeOne(int intNewAccRefNo, String strNewName, String `strNewAddress) 
    { 
     intAccRefNo = intNewAccRefNo; 
     strName = strNewName; 
     strAddress = strNewAddress; 

    }//end of overloading contructor 

    public String deposit(double dblDepositAmount) 
    { 
     dblBalance = dblBalance - dblDepositAmount; 

     return "Balance updated"; 
    } 

在我的程序類我已經寫:

 System.out.println("Enter deposit amount"); 
     dblDepositAmount=input.nextDouble(); 
     firstAccount.deposit(dblDepositAmount); 

但在存款法我的對象類的我都問了一個字符串說回「餘額更新「將被退回。

當我運行測試時,沒有字符串返回。把我的頭從桌子上撥開 - 我做了一件荒謬的事情嗎?

+0

您將需要打印返回值:-) – Bart 2013-03-20 18:34:00

回答

1

這行代碼丟棄調用deposit方法的結果,所以你沒有看到那個字符串:

firstAccount.deposit(dblDepositAmount); 

儘量不要使用以下:

System.out.println(firstAccount.deposit(dblDepositAmount)); 
+0

謝謝!大大的幫助 – 2013-03-20 18:38:15

3

你做了什麼來打印字符串:

1-使用您的輸出並打印它:

System.out.println("Enter deposit amount"); 
dblDepositAmount=input.nextDouble(); 
String myString = firstAccount.deposit(dblDepositAmount); //<-- you store your string somewhere 
System.out.println(myString); // you print your String here 

System.out.println(firstAccount.deposit(dblDepositAmount)); // Or you can show it directly 

2 - 你也可以讓你的方法打印出值

public void deposit(double dblDepositAmount) 
{ 
    dblBalance = dblBalance - dblDepositAmount; 

    System.out.println("Balance updated"); 
} 

所以,當你調用它,它就會自行打印(返回一個字符串值,你的情況沒用)。

+0

不客氣:) – 2013-03-20 18:41:06

相關問題