0

I need to converted from seconds to hours, minutes and seconds.
(without using if)

and this is my code

import java.util.Scanner;

public class SecondToHoursAndMin {

	public static void main (String[]ages)
	{
	Scanner in = new Scanner (System.in);
	
	int second;

	System.out.println("Enter the second: ");
	
	second = in.nextInt();
	
	int hours,minutes,second1;
	
	hours=(second/3600);
	
	minutes=(second%60);
	
	second1=((second%60)%60);
	
	System.out.println("The total of hours: "+hours);
	System.out.println("The total of minutes: "+minutes);
	System.out.println("The total of second: "+second1);	
	
}
}
4
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by armsracer
0

what input are you using, and what output are you getting.

input = seconds
output = hours , minutes , seconds .

0

input = seconds
output = hours , minutes , seconds .

I realize that, but i mean what was the number you were manually entering, and were you getting any results at all or were there errors?

Also, it looks like you need a space between String[] and ages in your main method.

0

What don't you try this? There are more efficient ways of doing this than what i outlined but sure it works :P

import java.util.Scanner;

public class SecondToHoursAndMin {

    public static void main (String[]ages)
    {
    Scanner in = new Scanner (System.in);

    float second;

    System.out.println("Enter the seconds: ");
    second = in.nextInt();

    float hours = (second/3600);
    System.out.printf("Hours: %.3f", hours);            
        // Use printf if we are formatting the output

    float minutes = (second/60);
    System.out.printf("\nMinutes: %.3f", minutes); 

    }
}

Edited by mike_2000_17: Fixed formatting

0

Yes i know i should have placed my code in quotes, before anyone points that error out

import java.util.Scanner;
 
public class SecondToHoursAndMin {
 
	public static void main (String[]ages)
	{
	Scanner in = new Scanner (System.in);
 
	float second;
 
	System.out.println("Enter the seconds: ");
	second = in.nextInt();
 
	float hours = (second/3600);
	System.out.printf("Hours: %.3f", hours);			
// Use printf if we are formatting the output
	
	float minutes = (second/60);
	System.out.printf("\nMinutes: %.3f", minutes); 

	}
}
0

Oh and btw the reason that the code wasn't working properly was because all the data types for the variables were set as integers.

0

On lines 19 what you want to do is (seconds%3600)/60. On line 21 you want (seconds%3600)%60. Int or float it doesn't matter because integer devision rounds down.

Edited by armsracer: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.