亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我怎么寫這個方法?

我怎么寫這個方法?

烙印99 2021-10-20 14:36:15
我實際上正在嘗試創建一個鬧鐘,但我對如何編寫此方法有些困惑,這是迄今為止的方向和內容。/*** 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) {    }}
查看完整描述

1 回答

?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

我會對小時之前和之后的分鐘使用不同的條件,并用于String.format創建這些字符串:


public String formatMinutesInConversationalTone(AlarmClock clock) {

    if (clock == null) {

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

    }


    int minutes = clock.getMinutes();

    if (minutes == 0) {

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

    }


    if (minutes == 30) {

        return "It is half past the hour";

    }


    if (minutes < 30) {

        return String.format("It is %d minutes past the hour", minutes);

    }


    return String.format("It is %d minutes before the hour", (60 - minutes));

}


查看完整回答
反對 回復 2021-10-20
  • 1 回答
  • 0 關注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號