如何将 24:00 小时转换为 12:00 格式

发布于 2024-12-03 00:55:43 字数 1594 浏览 0 评论 0原文

我的伪代码作业需要帮助:将 2400 小时转换为 12 格式

设计一种算法,提示并接收 2400 格式的项目表达(例如 2305 小时),将其转换为 12 小时格式(例如 11.05pm)并显示已输入 9999 的哨兵时间。

Prompt for hrs, mins
Get hrs, mins                               
DOWHILE(hrs NOT = 99) AND (mins NOT = 99) // If hrs & mins not = to 99 then it will run if not it will stop
    IF (hrs = 00) THEN  // midnight. 0030. It will + 12 and display 12:30am
        format = am
        time = hrs + 12
        Display hrs, ":" , mins, format
        ELSE
        IF (hrs > 12) THEN  // afternoon. 1630. It will – 12 and display 4:30pm
            format = pm
            hrs = hrs – 12
            Display hrs, ":" , mins, format
        ELSE
            IF (hrs < 12) THEN      // from midnight 0100 to 1159. It will display AM
                format = am
                Display hrs, ":" , mins, format
                IF (hrs = 12) THEN      // if format is 1230. It will display 1230PM
                    format = pm
                    Display hrs, ":" , mins, format
                ENDIF
            ENDIF
        ENDIF
    ENDIF

    IF (hrs < 0) OR (hrs > 23) THEN // hrs less than 0 or more than 23 is error. 
        Display ‘Invalid hour input’
        IF (mins < 0) OR (mins >59) THEN    // mins less than 0 or more than 59 is error. 
            Display ‘Invalid mins input’
        ENDIF
    ENDIF

    Prompt for hrs, mins    // you prompt again , we are still in the loop until we hit 9999
    Get hrs, mins
ENDDO           // which stop here because it’s 9999

我做得正确吗?请指教。新同学来啦!非常感谢!

I need help with my pseudocode assignment: convert 2400 hours to 12 format

Design an algorithm that will prompt for and receive the item expresses in 2400 format (e.g. 2305 Hours), convert it to 12 hour format (e.g. 11.05pm) and display sentinel time of 9999 is entered.

Prompt for hrs, mins
Get hrs, mins                               
DOWHILE(hrs NOT = 99) AND (mins NOT = 99) // If hrs & mins not = to 99 then it will run if not it will stop
    IF (hrs = 00) THEN  // midnight. 0030. It will + 12 and display 12:30am
        format = am
        time = hrs + 12
        Display hrs, ":" , mins, format
        ELSE
        IF (hrs > 12) THEN  // afternoon. 1630. It will – 12 and display 4:30pm
            format = pm
            hrs = hrs – 12
            Display hrs, ":" , mins, format
        ELSE
            IF (hrs < 12) THEN      // from midnight 0100 to 1159. It will display AM
                format = am
                Display hrs, ":" , mins, format
                IF (hrs = 12) THEN      // if format is 1230. It will display 1230PM
                    format = pm
                    Display hrs, ":" , mins, format
                ENDIF
            ENDIF
        ENDIF
    ENDIF

    IF (hrs < 0) OR (hrs > 23) THEN // hrs less than 0 or more than 23 is error. 
        Display ‘Invalid hour input’
        IF (mins < 0) OR (mins >59) THEN    // mins less than 0 or more than 59 is error. 
            Display ‘Invalid mins input’
        ENDIF
    ENDIF

    Prompt for hrs, mins    // you prompt again , we are still in the loop until we hit 9999
    Get hrs, mins
ENDDO           // which stop here because it’s 9999

Am i doing correctly? Please advice. New student here! many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

儭儭莪哋寶赑 2024-12-10 00:55:43

好吧,我认为,根据你的教授期望你的伪代码是什么样子,你所拥有的应该可以正常工作。不过,有几行有点多余。您可以将对小时和分钟的越界检查合并到一个 IF 语句中。然后,您可以将 time 变量默认设置为 "am",这会将您的 IF - ELSE IF - ELSE 转换为单个 >如果-否则。哦,并不是说我确信这很重要,但是当 hours = 0 时,您可以不使用 hours = hours + 12,而是可以只使用 hours = 12. 再说一次,我认为你所拥有的应该可以正常工作。

编辑:啊...再次,不确定这是否重要,但有一种可能终止程序的方法也可能有用。否则,你似乎会永远陷入循环之中。

编辑2:这就是我要做的......

done = false

DOWHILE !done
    PROMPT hours, minutes
    GET hours, minutes

    IF hours < 0 OR hours > 23 OR minutes < 0 OR minutes > 60
        DISPLAY "Invalid Time"
    ELSE
        format = "AM"

        IF hours > 12
            format = "PM"
            hours = hours - 12
        ELSE IF hours == 0
            hours = 12
        ELSE IF hours == 12
            format = "PM"

        DISPLAY hours ":" minutes format
        ENDIF
    ENDIF
    PROMPT "Are you done?"
    GET done
ENDLOOP

Well, depending upon how your professor expects your pseudocode to look like, what you have should work fine, I think. A few of the lines are a bit redundant, though. You could combine the out-of-bounds checking of the hours and minutes into one IF statement. You could then set your time variable to "am" by default, which would turn your IF - ELSE IF - ELSE to a single IF - ELSE. Oh, and not that I'm sure it matters much, but rather than using hours = hours + 12 when hours = 0, you could probably just do hours = 12. Again, what you have should work just fine, I think.

EDIT: Ah... again, not sure if this matters, but have a way to possibly terminate the program might be useful, too. Otherwise, you'll be stuck in your loop forever, it seems.

EDIT 2: Here's what I would do...

done = false

DOWHILE !done
    PROMPT hours, minutes
    GET hours, minutes

    IF hours < 0 OR hours > 23 OR minutes < 0 OR minutes > 60
        DISPLAY "Invalid Time"
    ELSE
        format = "AM"

        IF hours > 12
            format = "PM"
            hours = hours - 12
        ELSE IF hours == 0
            hours = 12
        ELSE IF hours == 12
            format = "PM"

        DISPLAY hours ":" minutes format
        ENDIF
    ENDIF
    PROMPT "Are you done?"
    GET done
ENDLOOP
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文