以下代码:
import java.util.*;
public class Convert
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the duration of the event in seconds: ");
int duration = keyboard.nextInt();
int days;
int hours;
int minutes;
int seconds;
如果用户输入,我需要的是 - < 60秒,它将增加到秒 - > 60秒,它将添加到分钟 - > 3600秒,它将增加到几小时 - > 86400秒,它会增加几天。
所以,例如, 如果用户输入值3750(= 3600 + 120 + 30),则输出 应该是:
该活动持续0天1小时2分30秒
答案 0 :(得分:0)
public void seperateDuration(int dur)
{
if(dur >= 86400) //checks if duration is greater than 1 day
{
while(dur >= 86400) //decreases 86400 seconds from duration for each day and adds 1 to day counter
{
dur-=86400;
days++;
}
}
if(dur >= 3600) //decreases 3600 seconds from duration for each hour and adds 1 to hour counter
{
while(dur >= 3600)
{
dur-= 3600;
hours++;
}
}
if(dur >= 60) //decreases 60 seconds from duration for each minute and adds 1 to day minute
{
while(dur >= 60)
{
dur -= 60;
minutes++;
}
}
seconds += dur; //adds remaining duration to seconds (will be less than 60)
}
答案 1 :(得分:0)
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the duration of the event in seconds: ");
int duration = keyboard.nextInt();
int days;
int hours;
int minutes;
int seconds;
days = duration / 86400;
hours = duration % 86400 / 3600;
minutes = duration % 86400 % 3600 / 60;
seconds = duration % 86400 % 3600 % 60;
System.out.println("The event lasts "+days+" days "+hours+" hours "+minutes+" minutes and "+seconds+" seconds");
}
}