i'm trying to write a program that can convert seconds into days, hours, minutes, seconds. But I am trying to make it loop so that once converted the program will prompt the user to enter another number of seconds, or if the number is negative end the program. So far, my problem seems to be with my while loop since the number of seconds isnt necessarily 0 it keeps trying to solve and prompt. Here is my code:
import java.util.*;
public class Probleme3
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input seconds :");
int sec = in.nextInt();
int min;
int heure;
int jour;
while(sec != 0)
{
if(sec < 0)
{
}
else
{
min = sec /60;
sec = sec % 60;
heure = min /60;
min = min % 60;
jour = heure /24;
heure = heure % 24;
System.out.println( jour + ":" + heure + ":" + min + ":" + sec);
System.out.println("Input seconds :");
}
}
}
}
sec = in.nextInt();this is the part that goes at the beginning58 % 60 = 58 != 0istruemaking this an infinite loop. Replace thewhile (sec != 0)forwhile (sec > 0), then all%` for-. Same example:58 - 60 = -2 > 0isfalsethus breaking the loop.