Why my code is not working more than once?

0 votes
asked Feb 13, 2018 by jack

import java.util.Scanner;

public class Main {

  public static void main(String[] args) 

  {

    Scanner console = new Scanner(System.in);

    final int secondsInMinute = 60;

    final int secondsInHour = 3600;

    final int secondsInDay = 86400;

    int userSeconds,remainingSeconds;

    int minutes = 0;

    int hours = 0;

    int days = 0;

    int seconds = 0;

    

    System.out.println("Enter the number of seconds");

    userSeconds = console.nextInt();

    

    if (userSeconds >= 86400){

      days = userSeconds / 86400;

      remainingSeconds = userSeconds % 86400;

      if (remainingSeconds >= 3600){

        hours = remainingSeconds/3600;

        remainingSeconds = remainingSeconds%3600;

        if (remainingSeconds >= 60){

        minutes = remainingSeconds/60;

        remainingSeconds = remainingSeconds%60;

        seconds = remainingSeconds;

      }else{

        seconds = remainingSeconds;

      }

    }else if (remainingSeconds >= 60){

        minutes = remainingSeconds/60;

        remainingSeconds = remainingSeconds%60;

        seconds = remainingSeconds;

      }else{

        seconds = remainingSeconds;

      }

    }else if(userSeconds >= 3600){

        hours = userSeconds/3600;

        remainingSeconds = userSeconds%3600;

        if (remainingSeconds >= 60){

        minutes = remainingSeconds/60;

        remainingSeconds = remainingSeconds%60;

        seconds = remainingSeconds;

      }else{

        seconds = remainingSeconds;

      }

    }else if (userSeconds >= 60){

        minutes = userSeconds/60;

        remainingSeconds = userSeconds%60;

        seconds = remainingSeconds;

      }else{

        seconds = userSeconds;

      }

      

       System.out.println("You entered: " + days +"days"+hours+"hours" +minutes+ "minutes" + seconds +"seconds" );

       

  }

}

/*

Enter the number of seconds                                                                                 

600                                                                                                         

You entered: 0days0hours10minutes0seconds                                                                   

                                                                                                            

                                                                                                            

...Program finished with exit code 0                                                                        

Press ENTER to exit console.

*/

2 Answers

0 votes
answered Mar 5, 2018 by Daniel Stuckey (420 points)
do you want it to loop or what cuz i am cunfuzzeled
commented Mar 5, 2018 by Daniel Stuckey (420 points)
if you do want loop do
while (boolean loop = false)
0 votes
answered Mar 5, 2018 by Daniel Stuckey (420 points)
Do this to loop

import java.util.Scanner;

public class Main {

  public static void main(String[] args)

  {

    Scanner console = new Scanner(System.in);

    final int secondsInMinute = 60;

    final int secondsInHour = 3600;

    final int secondsInDay = 86400;

    int userSeconds,remainingSeconds;

    int minutes = 0;

    int hours = 0;

    int days = 0;

    int seconds = 0;

  int loop2 = 0;
  
  boolean loop = false;
  
    while (loop2 == 0){

    System.out.println("Enter the number of seconds");

    userSeconds = console.nextInt();

    

    if (userSeconds >= 86400){

      days = userSeconds / 86400;

      remainingSeconds = userSeconds % 86400;

      if (remainingSeconds >= 3600){

        hours = remainingSeconds/3600;

        remainingSeconds = remainingSeconds%3600;

        if (remainingSeconds >= 60){

        minutes = remainingSeconds/60;

        remainingSeconds = remainingSeconds%60;

        seconds = remainingSeconds;

      }else{

        seconds = remainingSeconds;

      }

    }else if (remainingSeconds >= 60){

        minutes = remainingSeconds/60;

        remainingSeconds = remainingSeconds%60;

        seconds = remainingSeconds;

      }else{

        seconds = remainingSeconds;

      }

    }else if(userSeconds >= 3600){

        hours = userSeconds/3600;

        remainingSeconds = userSeconds%3600;

        if (remainingSeconds >= 60){

        minutes = remainingSeconds/60;

        remainingSeconds = remainingSeconds%60;

        seconds = remainingSeconds;

      }else{

        seconds = remainingSeconds;

      }

    }else if (userSeconds >= 60){

        minutes = userSeconds/60;

        remainingSeconds = userSeconds%60;

        seconds = remainingSeconds;

      }else{

        seconds = userSeconds;

      }

      

       System.out.println("You entered: " + days +"days"+hours+"hours" +minutes+ "minutes" + seconds +"seconds" );
       
       System.out.print("Do you want to repeat? \nType 1 for yes and 2 for no:");
       loop2 = console.nextInt();
       
      if (loop2 != 2) loop2 = 0;

    }

  }

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...