【问题标题】:Accessors not printing out the correct output访问器没有打印出正确的输出
【发布时间】:2021-12-01 07:08:20
【问题描述】:

我正在尝试让我的程序打印下面程序中定义的 4 个变量(wholeNumber、decimalPart、positive、currencySymbol)中的每一个,每个变量都使用一个访问器,但是当我去运行程序而不是得到这个

预期输出:

1
2
true
$

我得到了这个。有人能解释一下是什么错误造成的吗?

CURRENT OUTPUT

代码

public class Q4 {

    int wholeNumber;
    int decimalPart;
    boolean positive;
    char currencySymbol;

    public Q4(int wholeNumber, int decimalPart, boolean positive, char currencySymbol){

    }
    

    public static void main(String[] args) {
        Q4 m = new Q4(1,2,true,'$');
        System.out.println(m.getWholeNumber());
        System.out.println(m.getDecimalPart());
        System.out.println(m.isPositive());
        System.out.println(m.getCurrencySymbol());
    }

    // Accessor
    public int getWholeNumber(){
        return(wholeNumber);
    }

    // Accessor
    public int getDecimalPart(){
        return(decimalPart);
    }

    // Accessor
    public boolean isPositive(){
        return(positive);
    }

    // Accessor
    public char getCurrencySymbol(){
        return(currencySymbol);
    }

}

【问题讨论】:

  • 你有一个空的构造函数。
  • 嗨!欢迎来到 StackOverflow。为了帮助他人,请将您的“当前输出”作为文本包含在您的问题中,而不是链接到外部服务。谢谢!

标签: java accessor


【解决方案1】:

您忘记在构造函数中设置成员变量,所以它们保留为默认值。

public Q4(int wholeNumber, int decimalPart, boolean positive, char currencySymbol){
    this.wholeNumber = wholeNumber;
    this.decimalPart = decimalPart;
    this.positive = positive;
    this.currencySymbol = currencySymbol;
}

【讨论】:

    猜你喜欢
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    相关资源
    最近更新 更多