java - 为什么我的方法不返回字符串?

标签 java string return

我正在使用程序类来尝试测试对象类中的方法以查看它们是否有效。这是一个燃气表读取系统,我正在尝试存钱来偿还客户所欠的部分余额。

我的对象类包括:

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/

相关文章:

java - 从批处理文件中调用 ant

java - Android 4.4 SDK 更新后 android-support-v7-appcombat jar 不匹配和应用程序崩溃

Python-if语句下的多个条件

java - 内存中带有文字字符串的 StringBuilder/StringBuffer

javascript - 返回 Express Router 中间件内部

c++ - unsigned _stdcall ....隐含一个int?

java - 使用 OpenSSL 验证 Java 签名类签名

java - 在 map 中组合两个不同的字符串

javascript - 提取动态字符串中的单词

c - 尝试将汇编器字节流作为 C native 函数执行