[英]Why aren't my methods returning the String?
I am using a program class to try to test the methods in my object class to see if they work. 我正在使用程序类来尝试测试对象类中的方法,以查看它们是否有效。 It is a gas meter reading system and I am trying to deposit money to pay off some of the balance of what the customer owes. 这是一个煤气表读取系统,我正在尝试存钱以偿还客户欠款的部分余额。
My object class consists of: 我的对象类包括:
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";
}
In my program class I have written: 在我的程序课上,我写了:
System.out.println("Enter deposit amount");
dblDepositAmount=input.nextDouble();
firstAccount.deposit(dblDepositAmount);
But in my object class in the deposit method I have asked for a string saying return "Balance updated" to be returned. 但是在我的对象类的deposit方法中,我要求输入一个字符串,说要返回return“ Balance Updated”。
When I run the test there is no string returned. 当我运行测试时,没有返回任何字符串。 Banging my head off the table - have I done something ridiculous? 我的头撞到桌子上-我做过什么荒唐的事吗?
You did nothing to print your string: 您没有执行任何操作来打印您的字符串:
1- use your output and print it: 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- You can also make your method print the value 2-您也可以使您的方法打印值
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). 因此,当您调用它时,它将自行打印(在您的情况下,返回String值是没有用的)。
This line of code discards the result of invoking deposit
method, therefore you do not see that string: 此行代码放弃调用deposit
方法的结果,因此看不到该字符串:
firstAccount.deposit(dblDepositAmount);
Try the following instead: 请尝试以下操作:
System.out.println(firstAccount.deposit(dblDepositAmount));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.