【问题标题】:nested if statement not working in SML嵌套的 if 语句在 SML 中不起作用
【发布时间】:2013-10-12 13:20:51
【问题描述】:

我是 SML 的新手,我编写了一个程序,它需要两年时间来比较它们,然后需要两个月来比较它们,最后是两个日期。

我遇到的问题是,如果这一年比第一年大,它应该停止并且是假的,但有些人不确定这是我的逻辑还是它继续检查月份和日期,然后返回真或假.

如果年份为假,我希望它只检查月份,如果月份为假,只检查日期。

fun is_older(year1 : int,  month1 : int, day1 : int, year2 : int, month2 : int, day2 : int) =
if year1 < year2 andalso year1 > 0
then true
else
    if month1 < month2 andalso month1 > 0 andalso month2 <= 12
    then true
else
    if day1 < day2 andalso day1 > 0 andalso day2 <= 31
    then true

else false;

【问题讨论】:

    标签: sml


    【解决方案1】:

    我假设您尝试比较一年中的两个日期,并返回真/假值。你所做的大部分是正确的。在您的第二个 if 语句中,您只想检查 month1&lt;month2 是否为 year1=year2。否则,即使 year1=2014 和 year2=2013,如果他们的月份与您的第二个 if 语句一致,您将获得真正的价值。

    同样,在您的第三个 if 语句中,您只想在 year1=year2 andalso month1=month2 时检查天数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      • 2021-11-20
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多