【问题标题】:Why does the return-statement not quit the method? [closed]为什么返回语句不退出方法? [关闭]
【发布时间】:2019-08-14 23:08:46
【问题描述】:

我正在使用 Junit/Java 完成一项任务。该方法应该转到正确的 if 语句,以常用日历的方式提前一天,然后退出整个方法。

我已经广泛搜索了这个问题。我找到的唯一页面,指向我正在尝试做的确切事情。很可能我错过了一些东西,我只是不知道是什么。当我通过调试器运行测试时,我看到 Java 确实进入了正确的语句,它只是“忽略”了返回。

protected final void advanceDay() {
    int[] highMonth = new int[]{1, 3, 5, 7, 8, 10, 12};
    boolean isMonth31 = false;

    for (int x : highMonth) {
        if (this.monthFirstDay == x) {
            isMonth31 = true;
        }
    }
    //checks if month has 31 days

    if (this.dayFristDay >= 30) {
        if (this.dayFristDay == 31) {
            this.dayFristDay = 1;
            this.monthFirstDay++;
            return;
        }
        //if it's the 31st, then proceed to the next month, the day is set to one.

        if (this.dayFristDay == 31 && this.monthFirstDay == 12) {
            this.dayFristDay = 1;
            this.monthFirstDay = 1;
            return;
        }
        //if it's december the 31st, set the date to january 1st

        if (isMonth31 && this.dayFristDay == 30) {
            this.dayFristDay++;
            System.out.println("");
            return;
        } 
        //if the month has 31 days, but it is the 30st, just advance the day.

        if (!isMonth31 && this.dayFristDay == 30) {
            this.monthFirstDay++;
            this.dayFristDay = 1;
            System.out.println("");
            return;
            //if the month has 30 days and it is the 30st, advance the month and set the day to one. 
        }

    }

    if (this.dayFristDay == 28 && this.monthFirstDay == 2) {
        this.monthFirstDay++;
        this.dayFristDay = 1;
        System.out.println("");
        return;
    }
    //if it's the 28st of february, advance to march the first.
    System.out.println("");
    this.dayFristDay++;
}

打印是调试器的断点。如果任何 if 语句为真,我将永远不会到达最后一次打印。但我一直在写最后一张,虽然它不应该这样做。

编辑:重现错误: // 在同一个包的不同类中使用 Cockpit testCP = new Cockpit(28, 2); testCP.advanceDay();

public class Cockpit {

private int dayFristDay;
private int monthFirstDay;

public Cockpit(int dayFristDay, int monthFirstDay) {
    this.dayFristDay = dayFristDay;
    this.monthFirstDay = monthFirstDay;
}

//advanceDay method as a above

     protected String getCurrentDay() {
         return this.dayFristDay + "-" + this.monthFirstDay;
     }
}

【问题讨论】:

  • return 将结束该方法。你有错误的假设。如果该方法没有结束,则说明您没有点击语句。放置一些打印以确认您是否输入,或者使用您的调试器。
  • 你到达if声明或他们的身体?
  • @Zabuza 打印和调试器都确认它进入了 februay-28-if 和最后一个,就好像 return 不存在一样。
  • @JayAberlour 发布了一个完整的最小示例,我们可以自己复制、粘贴和运行以重现问题。但我怀疑你能否做到这一点,因为如果 return 不起作用,我想现在有人会注意到的。
  • 这是一个非常复杂的解决问题的方法:只需定义一个包含月份长度的数组,然后检查 (day+1) 是否超过当前月份的长度。

标签: java return


【解决方案1】:
Cockpit testCP = new Cockpit(28, 2);    
this.testCP.advanceDay();

第 2 行没有在您在第 1 行创建的实例上调用 advanceDay。您是在某个成员变量引用的实例上调用它。

删除this

Ideone demo, showing that return works

【讨论】:

  • 没错,但与我的问题无关。没有这个也会发生错误。
  • @JayAberlour 那么您将不得不提供一个更好的示例来重现该问题,因为我复制并粘贴了您的代码,然后返回工作(答案中的链接)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多