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);

但是在我的存款方法的对象类中,我要求返回一个返回“余额更新”的字符串。

当我运行测试时,没有返回字符串。把我的头从桌子上敲下来——我做了什么可笑的事吗?

4

2 回答 2

3

You did nothing to print your string:

1- use your output and print it:

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- You can also make your method print the value

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

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

So when you call it, it will print by itself (returning a String value is useless in your case).

于 2013-03-20T18:33:31.337 回答
1

This line of code discards the result of invoking deposit method, therefore you do not see that string:

firstAccount.deposit(dblDepositAmount);

Try the following instead:

System.out.println(firstAccount.deposit(dblDepositAmount));
于 2013-03-20T18:33:28.363 回答