-1

So I almost have this code correct but it's not giving me the correct output for some reason and I can't figure out why. This is what I have:

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);

    }

}

That is what I have so far but I need the output to be

Enter number of seconds: 50391
Hours: 13
Minutes: 59
Seconds 51
4
  • 1
    I'd suggest reading How to debug small programs and How to be a Programmers; Learn to Debug. That said, debugging is specifically mentioned as off topic in the help center. You may also find a Java debugging with eclipse tutorial helpful.
    – user289086
    Commented Sep 16, 2014 at 21:27
  • one good thing to try is to check your math with a calculator if its giving bad results.
    – Dave
    Commented Sep 16, 2014 at 21:37
  • I upvoted Question because it clearly states in the Help Ask Section "1.Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." And it seems you have a clear question right here
    – Rika
    Commented Sep 16, 2014 at 21:39
  • You said "I need the output to be etc etc etc" I run the code and the numbers are what you wanted the output to be. Or are you talking about the Strings of the output.
    – Rika
    Commented Sep 16, 2014 at 22:02

1 Answer 1

0

Go through each step by hand (or even use System.out.printlns/logging at each step. I am not ashamed to say I do this a decade+ after learning coding)

hours = number_of_seconds - number_of_seconds%3600;

Evaluates to:

46800 = 50391 - 3591

The intended logic would in fact be

//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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.