Java:将秒的输入转换为小时/分钟/秒 [英] Java: Converting an input of seconds into hours/minutes/seconds

查看:376
本文介绍了Java:将秒的输入转换为小时/分钟/秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个练习题,摘自Lewis&的 Java软件解决方案:程序设计的基础. Loftus,第4版;问题PP2.6(这是

This is an exercise question taken from Java Software Solutions: foundations of program design by Lewis & Loftus, 4th edition ; Question PP2.6 (here is a link)

问题如下:创建一个项目,该项目读取代表秒数的值,然后将等量的时间打印为 小时,分钟和秒. (例如,9999秒是等效的 到2小时46分39秒.)"

Question is as follows: " Create a project that reads a value representing a number of seconds, then print the equivalent amount of time as a combination of hours, minutes, and seconds. (For example, 9999 seconds is equivalent to 2 hours, 46 minutes, and 39 seconds.)"

到目前为止,我已经尝试了以下方法

I have so far tried the following

public static void main(String[] args)
{
    Scanner scan = new Scanner(System.in);

    double totalSecs, seconds, minutes, hours;

    System.out.println("Enter number of seconds: ");

    totalSecs = scan.nextInt();

    hours = totalSecs/3600;
    minutes = (Math.abs(Math.round(hours)-hours))*60;
    seconds = (Math.abs(Math.round(minutes)-minutes))*60;

    System.out.print(hours + "\n" + minutes + "\n" + seconds);

}

答案出来了, 小时:2.7775 分钟:13.350000000000009 秒:21.00000000000051

我想做的是用小时的小数乘以60来得到分钟,然后重复该过程几秒钟.但是我很难弄清楚,因此(Math.abs)等的解决方案很杂乱.

What I want to do is take the decimals of, say, hours and multiply them by 60 to get minutes and repeat the process for seconds. I'm however having trouble figuring it out, hence the messy solution of (Math.abs) etc.

您会建议我进行哪些更改/添加?谢谢!

What would you recommend me to change/add? Thanks!

注意:这是一本面向初学者的书,因此,我没有学到比代码中已经讲过的更多的操作.因此,在以前的问题被问到的时候,我还不了解解决方案.

Note: This is a book for beginners, hence I've not learned many more operations than those I've already stated in the code. As such, I haven't understood the solution for the previous times this question has been asked.

推荐答案

这是一种更简单的方法:

Here is a much simpler way of doing it:

public static void main (String[] args) throws Exception {
    SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
    TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
    System.out.println(sf.format(new Date(9999000)));
}

注意:请注意,这只会以24小时格式显示输出,如果小时数大于24,则它将以一天为单位递增并重新开始.例如,30小时将显示为06:xx:xx.另外,您必须以毫秒为单位传递输入,而不是秒.

Note: Please note that this will only show the output in 24-hour format and if number of hours is greater than 24 then it'll increment a day and will start over. For example, 30 hours will be displayed as 06:xx:xx. Also, you'll have to pass the input in milliseconds instead of seconds.

如果您想按照自己的方式做,那么您应该做这样的事情:

If you want to do it your way then you should probably do something like this:

public static void main (String[] args) throws Exception {
    long input = 9999;
    long hours = (input - input%3600)/3600;
    long minutes = (input%3600 - input%3600%60)/60;
    long seconds = input%3600%60;
    System.out.println("Hours: " + hours + " Minutes: " + minutes + " Seconds: " + seconds);
}

输出:

Hours: 2 Minutes: 46 Seconds: 39

这篇关于Java:将秒的输入转换为小时/分钟/秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆