我怎么写这个方法?

我实际上正在尝试创建一个闹钟,但我对如何编写此方法有些困惑,这是迄今为止的方向和内容。


/**

* Formats the time one of the following ways:

* "It is the top-of-the-hour" (if the minutes is 0)

* "It is half-past the hour" (if the minutes is 30)

* "It is 23 minutes past the hour" (if the minutes is more than 0 and less than 30; use the correct number

* of minutes here (23 is just an example)

* "It is 10 minutes before the hour" (if the minutes is more than 30 and less than 60; use the correct number

* of minutes here (10 is just an example)

*

* @precondition clock != null

* @postcondition none

*

* @param clock the clock to format

* @return a string as described above

*/

public String formatMinutesInConversationalTone(AlarmClock clock) {

    if (clock == null) {

        throw new IllegalArgumentException("clock cannot be null");

    }

    if (clock.getMinutes() == 0) {

        return "It is the top-of-the-hour";

    }

    if(clock.getMinutes() == 30) {

    return "it is half past the hour";

    }

    if(clock.getMinutes() > 0 && clock.getMinutes() < 30) {


    }

}


烙印99
浏览 111回答 1
1回答

叮当猫咪

我会对小时之前和之后的分钟使用不同的条件,并用于String.format创建这些字符串:public String formatMinutesInConversationalTone(AlarmClock clock) {&nbsp; &nbsp; if (clock == null) {&nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("clock cannot be null");&nbsp; &nbsp; }&nbsp; &nbsp; int minutes = clock.getMinutes();&nbsp; &nbsp; if (minutes == 0) {&nbsp; &nbsp; &nbsp; &nbsp; return "It is the top-of-the-hour";&nbsp; &nbsp; }&nbsp; &nbsp; if (minutes == 30) {&nbsp; &nbsp; &nbsp; &nbsp; return "It is half past the hour";&nbsp; &nbsp; }&nbsp; &nbsp; if (minutes < 30) {&nbsp; &nbsp; &nbsp; &nbsp; return String.format("It is %d minutes past the hour", minutes);&nbsp; &nbsp; }&nbsp; &nbsp; return String.format("It is %d minutes before the hour", (60 - minutes));}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java