Java: Converting an input of seconds into hours/minutes/seconds

后端 未结 2 1621
生来不讨喜
生来不讨喜 2021-01-27 06:01

This is an exercise question taken from Java Software Solutions: foundations of program design by Lewis & Loftus, 4th edition ; Question PP2.6 (here is a link)

2条回答
  •  执笔经年
    2021-01-27 06:21

    As an alternative of user2004685's answer;

    int seconds = 9999; 
    int hour = 9999 / (60 * 60); //int variables holds only integer so hour will be 2
    seconds = 9999 % (60 * 60); // use modulo to take seconds without hours so it will be 2799
    int minute = seconds / 60; //same as int variable so minute will be 49
    seconds = seconds % 60; // modulo again to take only seconds
    System.out.println(hour + ":" + minute + ":" + seconds);
    

提交回复
热议问题