我正在使用程序类来尝试测试对象类中的方法以查看它们是否有效。这是一个燃气表读取系统,我正在尝试存钱来偿还客户所欠的部分余额。
我的对象类包括:
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);
但是在我的存款方法的对象类中,我要求返回一个字符串,表示返回“余额已更新”。
当我运行测试时,没有返回任何字符串。把我的头从 table 上撞下来 - 我做了什么可笑的事情吗?
最佳答案
您没有执行任何操作来打印字符串:
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");
}
因此,当您调用它时,它会自行打印(在您的情况下返回字符串值是无用的)。
关于java - 为什么我的方法不返回字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15531871/