我不得不将秒转换为H:M:S,这相当于一次考试中的30分,但为了“效率”,我被扣了3分。为什么?
import javax.swing.JOptionPane;
public class secToMin{
public static void main(String[] args){
int sec, secTotal, hour, min, rem;
secTotal = Integer.parseInt(JOptionPane.showInputDialog("Enter number of seconds"));
if (secTotal<0)
{
System.out.println("invalid input");
System.exit(0);
}
hour = (secTotal/3600);
rem = (secTotal%3600);
min = (rem/60);
sec = (rem%60);
JOptionPane.showMessageDialog(null, secTotal + " equals " + hour + ":" + min + ":" + sec + ".");
System.exit(0);
}
}发布于 2018-01-18 10:25:22
考试的潜在解决方案可能是避免像"3600“这样的魔术数字(好的,这是一个众所周知的值,但它是一个很大的值)。
相反,由the National Institute of Standards and Technology定义的International System of Units中的Units of time将简单地告诉您,一小时是"60分钟“,一分钟是"60秒”。
您可以使用以下命令实现相同的转换步骤:
min = secTotal / 60;
hour = min / 60;
min = min % 60;
sec = secTotal % 60;https://stackoverflow.com/questions/33266333
复制相似问题