设定输入的输出不正确

时间:2014-09-16 21:23:11

标签: java

所以我几乎把这段代码改正了,但由于某些原因它没有给我正确的输出,我无法弄清楚原因。这就是我所拥有的:

package edu.ilsu;

import java.util.Scanner;
/**
 * @author Nick
 */
public class Seconds {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int hours, minutes, number_of_seconds, seconds;
        System.out.print("50391");
        number_of_seconds = in.nextInt();
        hours=number_of_seconds-number_of_seconds%3600;
        seconds=number_of_seconds%60;
        minutes=(number_of_seconds-hours-seconds)/60;
        hours=hours/3600;
        System.out.println("hours: " + hours);
        System.out.println("minutes: " + minutes);
        System.out.println("seconds: " + seconds);

    }

}

这就是我到目前为止,但我需要输出

Enter number of seconds: 50391
Hours: 13
Minutes: 59
Seconds 51

1 个答案:

答案 0 :(得分:0)

手动完成每一步(甚至在每一步都使用System.out.printlns / logging。在学习编码后,我并不羞于说我做了十年+)

hours = number_of_seconds - number_of_seconds%3600;

评估为:

46800 = 50391 - 3591

预期的逻辑实际上是

//pseudocode, may have to floor or whatnot, but this should get you in right direction
hours = number_of_seconds/3600;
number_of_seconds -= hours*3600;