**6.25(将毫秒数转换成小时数、分钟数和秒数)使用下面的方法头,编写一个将毫秒数转换成小时数、分钟数和秒数的方法。
public static String convertMillis(long millis)
该方法返回形如“小时:分钟:秒”的字符串。例如:convertMillis(5500)返回字符串0:0:5,convertMillis(100000)返回字符串0:1:40,convertMillis(555550000)返回字符串154:19:10。编写一个测试程序,提示用户输入一个long型的毫秒数,以“小时:分钟:秒”的格式显示一个字符串。
**6.25(Convert milliseconds to hours, minutes, and seconds)Write a method that converts milliseconds to hours, minutes, and seconds using the following header:
public static String convertMillis(long millis)
The method returns a string as hours:minutes:seconds. For example, convertMillis(5500) returns a string 0:0:5, convertMillis(100000)returns a string 0:1:40, and convertMillis(555550000) returns a string 154:19:10. Write a test program that prompts the user to enter a long integer for milliseconds and displays a string in the format of hours:minutes:seconds.
下面是参考答案代码:
// https://cn.fankuiba.com public class Ans6_25_page202 { public static void main(String[] args) { System.out.println(convertMillis(555550000)); System.out.println(convertMillis(100000)); System.out.println(convertMillis(5500)); } public static String convertMillis(long millis) { long totalSecond = millis / 1000; long currentSecond = totalSecond % 60; long totalMinute = totalSecond / 60; long currentMinute = totalMinute % 60; long totalHour = totalMinute / 60; return totalHour+":"+currentMinute+":"+currentSecond; } }
适用Java语言程序设计与数据结构(基础篇)(原书第11版)Java语言程序设计(基础篇)(原书第10/11版) 更多内容