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

しかし、deposit メソッドのオブジェクト クラスでは、return "Balance updated" という文字列が返されるように要求しました。

テストを実行すると、文字列が返されません。テーブルから頭をぶつけて - 私はばかげたことをしましたか?

4

2 に答える 2

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

したがって、それを呼び出すと、それ自体が出力されます(あなたの場合、文字列値を返すことは役に立ちません)。

于 2013-03-20T18:33:31.337 に答える
1

このコード行は、メソッドを呼び出した結果を破棄するため、depositその文字列は表示されません。

firstAccount.deposit(dblDepositAmount);

代わりに次のことを試してください。

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