第VI章質問25(ミリ秒を時間、分、秒に変換する)

第VI章質問25(ミリ秒を時間、分、秒に変換する)

  • ** 6.25(ミリ秒を時間、分、秒に変換する)以下のメソッドヘッダーを使用して、ミリ秒を時間、分、秒に変換するメソッドを記述します。
    public static String convertMillis(long millis)
    このメソッドは、「hour:minute:second」の形式の文字列を返します。例:convertMillis(5500)は文字列0:0:5を返し、convertMillis(100000)は文字列0:1:40を返し、convertMillis(555550000)は文字列154:19:10を返します。ユーザーに長いミリ秒の入力を求め、「時:分:秒」の形式で文字列を表示するテストプログラムを作成します。
    ** 6.25(ミリ秒を時間、分、秒に変換)次のヘッダーを使用して、ミリ秒を時間、分、秒に変換するメソッドを記述します
    。publicstatic String convertMillis(long millis)
    このメソッドは、文字列を時間:分:秒として返します。たとえば、convertMillis(5500)は文字列0:0:5を返し、convertMillis(100000)は文字列0:1:40を返し、convertMillis(555550000)は文字列154:19:10を返します。ユーザーにミリ秒単位の長整数の入力を求め、時間:分:秒の形式で文字列を表示するテストプログラムを作成します。
  • 参照コード:
package chapter06;

import java.util.Scanner;

public class Code_25 {
    
    
    public static void main(String[] args) {
    
    
        Scanner inputScanner = new Scanner(System.in);
        System.out.print("enter a long integer for milliseconds: ");
        long millis = inputScanner.nextLong();
        System.out.printf("The time is %s", convertMillis(millis));
    }
    public static String convertMillis(long millis) {
    
    
        String timeString = "";
        long totalSeconds = millis / 1000;
        long currentSecond = totalSeconds % 60;
        long totalMinutes = totalSeconds / 60;
        long currentMinute = totalMinutes % 60;
        long totalHours = totalMinutes / 60;
        timeString = String.valueOf(totalHours) + ":" + String.valueOf(currentMinute) + ":" + String.valueOf(currentSecond);
        return timeString;
    }
}

  • 結果は次のことを示しています。
enter a long integer for milliseconds: 555550000
The time is 154:19:10
Process finished with exit code 0

おすすめ

転載: blog.csdn.net/jxh1025_/article/details/109169546