0

对于以下代码:

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秒

4

2 回答 2

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)
}
于 2013-11-07T01:19:30.423 回答
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");
}
}
于 2013-11-07T01:21:06.250 回答