Java使用唯一输出将秒数转换为天,小时,分钟,秒

时间:2018-02-22 23:42:58

标签: java converter

所以我需要将秒数转换为天,小时分钟秒。

但我需要有时间的唯一输出,如果它们返回零,则不在响应中包含它们。

因此,如果输入3600秒,则需要返回1小时而不是其他任何内容。如果返回响应0,如何跳过一行。

这是我到目前为止的代码。

import java.util.Scanner;

public class ps9 {
public static void main(String [] args){
    Scanner in = new Scanner(System.in);
    System.out.println("Enter number of seconds");
    int input = in.nextInt();

    int numberOfDays;
    int numberOfHours;
    int numberOfMinutes;
    int numberOfSeconds;

    numberOfDays = input / 86400;
    numberOfHours = (input % 86400 ) / 3600 ;
    numberOfMinutes = ((input % 86400 ) % 3600 ) / 60;
    numberOfSeconds = ((input % 86400 ) % 3600 ) % 60;

    if (numberOfDays > 0) {
        System.out.println(numberOfDays + " day(s)");
        if (numberOfHours != 0)
        System.out.println(numberOfHours + " hour(s)");
        nextLine;
        System.out.println(numberOfMinutes + " minute(s)");
        System.out.println(numberOfSeconds + " second(s)");
    }
     else if (numberOfHours >= 0 )
    {
        System.out.println(numberOfHours +" hour(s)");
        System.out.println(numberOfMinutes + " minute(s)");
        System.out.println(numberOfSeconds + " second(s)");
    }
    else if (numberOfMinutes >= 0 )
    {
        System.out.println(numberOfMinutes + " minute(s)");
        System.out.println(numberOfSeconds + " second(s)");
    }
    else if (numberOfSeconds >= 0)
    {
        System.out.println(numberOfSeconds + " second(s)");
    }
}

}

2 个答案:

答案 0 :(得分:2)

由于您只想在输出为0时跳过输出,因此请使用一系列if语句而不是if-else,因为您希望彼此独立地执行这些语句。

这将减少可能的代码重复

Duration d = Duration.ofSeconds(3660);
long days = d.toDays();
d = d.minusDays(days);
long hours = d.toHours();
d = d.minusHours(hours);
long mins = d.toMinutes();
d = d.minusMinutes(mins);
long seconds = d.getSeconds();

if (days > 0) {
    System.out.println(days + " day(s)");
}
if (hours > 0) {
    System.out.println(hours + " hour(s)");
}
if (mins > 0) {
    System.out.println(hours + " min(s)");
}
if (seconds > 0) {
    System.out.println(hours + " second(s)");
}

注意,我很懒,而且涉及日期/时间操作,所以我使用了可用的库

答案 1 :(得分:0)

您可以检查此代码,但是我没有包括几秒钟。我从minutes开始。

在几秒钟内,您可以使用timeInterval来获得%秒。

public static final String INVALID_MESSAGE = "Invalid Value";
 
public static void printYearsAndDays(long minutes) {
    if (minutes < 0) {
        System.out.println(INVALID_MESSAGE);
    } else {
 
        int timeInterval = 60;
        int dayInterval = 24;
        int yearInterval = 365;
        long remainingMinutes = minutes % timeInterval;
        long remainingHours = (minutes / timeInterval) % dayInterval;
        long remainingDays = ((minutes / timeInterval) / dayInterval) % yearInterval;
        long remainingYears = ((minutes / timeInterval) / dayInterval) / yearInterval;
 
        String minutesDisplay = remainingMinutes + "min ";
        String hoursDisplay = remainingHours + "h ";
        String daysDisplay = remainingDays + " d";
        String yearsDisplay = remainingYears + " y";
 
 
        System.out.println(minutes+ " min = "+ yearsDisplay + " and " + daysDisplay);
    }
}