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

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

input = seconds
output = hours , minutes , seconds .

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.

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 3 Years Ago by mike_2000_17: Fixed formatting

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

	}
}

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.

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 6 Years Ago by armsracer: n/a

This article has been dead for over six months. Start a new discussion instead.