Quick simple question

burton_o61

Daemon Poster
Messages
968
I'm in a java j++ class. And i need to write a class that basically needs to turn 197 minutes into hours and minutes. I know that this is a simple task, i just cant think of it. What i need, is the output to be 3hrs, 17 minutes but it just comes out as:
Time worked is 3.283333333333333 hours and 3.283333333333333


I know that is should look somthing like this. Thanks again.

public class Time
{
public static void main(String[] args)
{
double Minute, Hour, Modulus;
Minute = 197;
Hour = Minute / 60;
Modulus = Hour % 60;
System.out.println ("Time worked is " + Hour);
System.out.println (" hours and " + Modulus);
}
}

Ok i found ONE problem. It should be minute % 60. But the hour still shows .283333333333333. It should show 3.
public class Time
{
public static void main(String[] args)
{
double Minute, Hour, Modulus;
Minute = 197;
Hour = Minute / 60;
Modulus = Minute % 60;
System.out.println ("Time worked is " + Hour);
System.out.println (" hours and " + Modulus);
}
}

And just like that i figured it out. When i have a double it uses decimals. And that is why im getting the decimal for my hour. When i change it from a double to an int. It only displays whole numbers. I knew it was an easy fix but i just chouldnt think at the time.

public class Time
{
public static void main(String[] args)
{
int Minute, Hour, Modulus;
Minute = 197;
Hour = Minute / 60;
Modulus = Minute % 60;
System.out.println ("Time worked is " + Hour);
System.out.println (" hours and " + Modulus);
}
}
 
Back
Top Bottom