javascript - 如何使用模数将秒转换为小时分钟和秒?

标签 javascript eclipse joptionpane

我的老师给我布置了这个作业,她的提示是“编写一个程序,允许用户输入秒数。该程序应该计算出等效的小时数、分钟数和秒数。示例:9999秒=2小时,46 分 39 秒。Mod 是你的 friend 。我正在尝试使用 JOptionPane 来实现这一点。

我尝试了很多其他方法,但都不起作用。当我输入 2 时,结果是 2 小时。

我预计当我输入 2 时,结果会是 2 秒,而不是几个小时!

最佳答案

我怀疑你想做这样的事情: 给定 n 秒、var 小时、var 分钟、var 秒

seconds = n%60;
minutes = (n-seconds)%3600;
//we use subtract seconds that have already been allocated
//Mod by 3600 here because this is the number of seconds in 1 hour, so the remainder will not fit into an hour
minutes = minutes/60; //convert seconds to minutes
hours = (n - minutes*60 - seconds) / 3600; //seconds remaining/seconds in an hour

该解决方案使用 mod 工作,并且应该工作 2 秒。您可能需要添加一些条件 IF 语句以确保除 0 时不会失败。

关于javascript - 如何使用模数将秒转换为小时分钟和秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58715485/

相关文章:

java - 是否有 Eclipse 交互式调试工具来评估类型属性?

java - JOptionPane 对话框错误

javascript - AngularJS 双向数据绑定(bind)被 setTimeout 取消

javascript - 覆盖 JavaScript 数组的打印表示

Javascript:如何清除非全局(封闭)setTimeout?

java - 运行程序并输入正确的输入后,程序不会显示程序的答案

java - 在 JOptionPane 上设置 DocumentFilter

javascript - 我的算法可以被认为是 O(n) 吗? "Finding the longest substring with unique characters"

c++ - .gdbinit 文件丢失

java - 如何自定义Eclipse生成的hashCode()和equals()?