【问题标题】:Why does my method increment seemingly automatically?为什么我的方法看似自动递增?
【发布时间】:2021-10-07 22:50:37
【问题描述】:

我一直在研究“Head First Java”,并在看似相当简单的问题上花费了大量时间。然而,我一生都无法弄清楚为什么 maybeNew 方法看似随机递增。我花了很多时间假设它是死代码。

在这种情况下,return 1 是否也可以用作索引增加器?

代码输出为 14 1。

书中的问题是计算给定以下代码会输出哪些不同的流控制示例:例如例如,x

count = count + m4a[x].maybeNew(x);

感谢您的耐心等待。

public class Mix4 {
    int counter = 0;

    public static void main(String[] args) {
        int count = 0;
        Mix4[] m4a = new Mix4[20];
        int x = 0;
        while (x < 9) {
            m4a[x] = new Mix4();
            m4a[x].counter = m4a[x].counter + 1;
            count = count + 1;
            count = count + m4a[x].maybeNew(x);
            x = x + 1; // 1;, 2;
        }
        System.out.println(count + " " + m4a[1].counter);
    }

    public int maybeNew(int index) { // index = 0;
        if (index < 5) {
            Mix4 m4 = new Mix4(); // m4 0 - 4; rn = 1;
            m4.counter = m4.counter + 1; // 1.
            // System.out.println(index);
            return 1;
        }
        return 0;
    }
}

【问题讨论】:

  • 当您说“方法递增”时,我不明白您的意思。你的意思是一些变量的值递增,当方法运行时?如果是这样,哪个,以及你为什么不期待这个? “代码输出为 14 1。”请准确解释您认为答案应该是什么,并逐步解释您的逻辑。您的代码 cmet 不足以让我理解您的想法。
  • 是的,也不确定你所说的“方法增量”是什么意思,maybeNew 总是return 1 或 0。
  • count = count + 1; count = count + m4a[x].maybeNew(x); 这两行将始终将count 增加1 或2,具体取决于maybeNew 是返回1 还是0。这是您有疑问的地方吗?
  • 你特意调出了count = count + m4a[x].maybeNew(x);这行代码。你的意思是你对这条线上发生的事情感到困惑吗?根据您调试代码的尝试,您认为这一行似乎发生了什么?你认为应该怎么做?
  • 如果 Java 是您尝试学习的第一个编程语言,那么 Head First Java 不是您应该使用的书。它在介绍部分明确告诉您

标签: java methods return increment return-type


【解决方案1】:

如果这可能有帮助:

public int maybeNew(int index) { // index = 0;
    if (index < 5) {
        Mix4 m4 = new Mix4(); // m4 0 - 4; rn = 1;
        m4.counter = m4.counter + 1; // 1.
        // System.out.println(index);
        return 1;
    }
    return 0;
}

整个new Mix4() 在这里毫无用处:这是一个死变量,因为您实际上并没有对m4 做任何事情。

所以maybeNew可以写成:

public int maybeNew(int index) { // index = 0;
    if (index < 5) {
        return 1;
    }
    return 0;
}

整个maybeNew方法也可以是static(独立于Mix4的任何实例):

    int count = 0;
    Mix4[] m4a = new Mix4[20];
    int x = 0;
    while (x < 9) {
        m4a[x] = new Mix4();
        m4a[x].counter = m4a[x].counter + 1;
        count = count + 1;
        count = count + Mix4.maybeNew(x);
        x = x + 1; // 1;, 2;
    }
    System.out.println(count + " " + m4a[1].counter);

由于代码只使用过 m4a[1],因此不使用其他索引;您可以移动 new Mix4 并进一步:简化循环:

    int count = 0;
    Mix4[] m4a = new Mix4[20];
    m4a[1] = new Mix4();
    m4a[1].counter = m4a[1].counter + 1;
    int x = 0;
    while (x < 9) {
        count = count + 1 + Mix4.maybeNew(x);
        x = x + 1; // 1;, 2;
    }
    System.out.println(count + " " + m4a[1].counter);

最后,移除数组:

    int count = 0;
    Mix4 m4a = new Mix4();
    m4a.counter = m4a.counter + 1;
    int x = 0;
    while (x < 9) {
        count = count + 1 + Mix4.maybeNew(x);
        x = x + 1; // 1;, 2;
    }
    System.out.println(count + " " + m4a.counter);

然后可以读取循环:

  • count 加一 9 次
  • 添加maybeNew(x)的结果:
    • 也许是新的(0):1
    • 也许是新的(1):1
    • 也许是新的(2):1
    • 也许是新的(3):1
    • 也许是新的(4) : 1
    • 也许是新的(5):0
    • 也许是新的(6):0
    • 也许是新的(7):0
    • 也许是新的(8):0
    • 将 5 添加到 count

= 计数 = 14。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-11
    • 2015-02-18
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 2012-12-28
    • 2022-11-10
    • 2018-06-05
    相关资源
    最近更新 更多