convert a 4-digit military time into the standard 12 hour time format

前端 未结 4 2026
半阙折子戏
半阙折子戏 2020-12-18 03:53

What I\'m trying to do:

I\'m trying to convert a 4-digit military time into the standard 12 hour time format, with a colon and an added PM or AM wit

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-18 04:39

    if (pm = true)
    if (pm = false)
    

    A single equal sign is an assignment. You want to do a comparison:

    if (pm == true)
    if (pm == false)
    

    These could also be written more idiomatically as:

    if (pm)
    if (!pm)
    

    That's the main problem. Another is your logic for setting pm. You only need a single if statement, with the line subtracting 1200 placed inside the pm = true block.

    if (milTime < 1200)
    {
        pm = false;
    }
    else
    {
        pm = true;
        milTime -= 1200;
    }
    

    There are a few other bugs that I'll leave to you. I trust these corrections will get you a bit further, at least.

提交回复
热议问题