2013-03-20 75 views
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); 

但在存款法我的对象类的我都问了一个字符串说回“余额更新“将被退回。

当我运行测试时,没有字符串返回。把我的头从桌子上拨开 - 我做了一件荒谬的事情吗?

+0

您将需要打印返回值:-) – Bart 2013-03-20 18:34:00

回答

1

这行代码丢弃调用deposit方法的结果,所以你没有看到那个字符串:

firstAccount.deposit(dblDepositAmount); 

尽量不要使用以下:

System.out.println(firstAccount.deposit(dblDepositAmount)); 
+0

谢谢!大大的帮助 – 2013-03-20 18:38:15

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

所以,当你调用它,它就会自行打印(返回一个字符串值,你的情况没用)。

+0

不客气:) – 2013-03-20 18:41:06

相关问题