我的伪代码分配需要帮助:将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
我做得对吗?请指教。这里有新生!非常感谢!
答案 0 :(得分:0)
那么,根据你的教授希望你的伪代码看起来如何,我认为你应该做得很好。但是,有些线路有点多余。您可以将小时和分钟的越界检查合并到一个IF
语句中。然后,您可以将time
变量默认设置为"am"
,这会将IF - ELSE IF - ELSE
变为单IF - ELSE
。哦,不是我确定这很重要,而是在hours = hours + 12
时使用hours = 0
,而不是hours = 12.
,你可能只做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
再一次,你所拥有的应该工作得很好,我认为
编辑2:这就是我要做的......
{{1}}