我正在使用程序类来尝试测试对象类中的方法以查看它们是否有效。这是一个燃气表读数系统,我试图存钱以偿还客户所欠的一些余额。
我的对象类包括:
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 :(得分: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");
}
因此,当您调用它时,它将自行打印(在您的情况下返回String值是无用的)。
答案 1 :(得分:1)
这行代码会丢弃调用deposit
方法的结果,因此您看不到该字符串:
firstAccount.deposit(dblDepositAmount);
请尝试以下方法:
System.out.println(firstAccount.deposit(dblDepositAmount));