Search code examples
javatimerstopwatch

I want to be able to make something like a background stopwatch?


I'm kind of new to Java so please bear with me. So I'm currently making a program where in when the user starts the program a background stopwatch/timer will run. While the timer is running the user will then input commands and those commands will display a time. I'm trying to make something like a respawn timer where the user won't see the time while it is running but when the user inputs the command of a monster it will show when it will respawn.

This program won't be having any GUI and will display everything in words.

Here is what I have right now:

        System.out.println("Start?");
    char s = in.next().charAt(0);
    if (s == 'S')
    {           
        do
        {
            System.out.println("Enter camp number:");
            char cn = in.next().charAt(0);

            if (cn == 'A')
                System.out.println("Raptor's respawn time is at " + min + ":" + sec + "\n");
            else if (cn == 'B')
                System.out.println("Wolf's respawn time is at " + min + ":" + sec + "\n");
            else if (cn == 'X')
                {
                    x = true;
                    System.out.println("Thank you for using my program!");
                }
            else
                System.out.println("Invalid input");
        }
        while (!x);
    }
    else
        System.out.println("Invalid input, please restart program.");
}

So when the user inputs S the timer/stopwatch will start running and when the user inputs either A or B the program will display the time they will respawn.

Advance thank you! :D


Solution

  • You can do something like this:

    Calendar c = Calendar.getInstance();
    int hrs = c.get(Calendar.HOUR);
    int mnts = c.get(Calendar.MINUTE);
    int secs = c.get(Calendar.SECOND);
    

    Then you can print the re-spawn time by printing

    System.out.printf("Monster will respawn in %d minutes and %d seconds", mnts+min, secs+sec);
    

    Of course you will also have to do some validation, like if secs+sec gives you more than 60, add one to the minutes and keep the remaining amount for the seconds.