Study Java: Use Timer to create something like a bomb timer

I suddenly remembered Google's 15-minute rule I tried to see if it can be described as a timer in java

I haven't come up with the refreshing introduction to Java that I'm currently reading, so I'd like to see the API or refer to Qiita. It's been about 2 weeks since I started learning java, so I tried splitting the code

Main



import java.util.*;

public class Main {
    static Scanner inp = new Scanner(System.in);
    public static void main(String[] args) {
        try {
            System.out.print("Let's set up a bomb timer(sec): ");
            int sec = inp.nextInt();
            BombTimer.Bomb(sec);
        } catch (InputMismatchException e) {
            System.out.println("The input is incorrect.");
        } finally {
            inp.close();
        }
    }
}

BombTimer



import java.util.*;

public class BombTimer {
    public static void Bomb(int sec) {
            System.out.println("Until the explosion" + sec + "Seconds");
            Timer timer = new Timer();
            TimerTask task = new TimerTask() {
                int secCount = sec - 1;
                @Override 
                public void run() {
                    if (secCount > 0) {
                        if (secCount <= 60 && secCount % 10 == 0) {
                            System.out.println("remaining" + secCount + "Seconds");
                        } else if (secCount == 5) {
                            System.out.println(secCount + "Seconds ago");
                        } else {
                            System.out.println(sec);
                        }

                    } else {
                        System.out.println("Doka --------");
                        System.exit(0);
                    }
                    secCount --;
                }
            };
            timer.sucheduleAtFixedRate(task, 1000, 1000);
        }
}

The execution result is as follows

result


Set up a bomb timer(sec): 13
13 seconds left until the explosion
12
11
10 seconds left
9
8
7
6
5 seconds ago
4
3
2
1
Doka --------!

What I learned this time -@Override is a description --Scanner close () --You can understand how to use it by looking at the API reference.

I used it as a reference

[Java] Timer processing implementation method

Summary

I've figured it out, but I still don't know the correct answer. Should it be an if statement or a switch statement? Is exception handling mandatory? I have no end to my doubts. .. ..

Please do not hesitate to point out any points you notice.

Recommended Posts

Study Java: Use Timer to create something like a bomb timer
[Java] Create something like a product search API
[Java] How to create a folder
Implement something like a stack in Java
Let's create a timed process with Java Timer! !!
Try to create a bulletin board in Java
[Java] Create a filter
How to create a Java environment in just 3 seconds
I want to do something like "cls" in Java
I tried to create a Clova skill in Java
How to create a data URI (base64) in Java
[Java] How to use Map
I tried to create a java8 development environment with Chocolatey
[Java] How to use Map
Java conditional branching: How to create and study switch statements
How to create a placeholder part to use in the IN clause
Create a java method [Memo] [java11]
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] Create a temporary file
How to use Java Map
[java] Reasons to use static
How to use Java variables
[Java] How to use Optional ①
How to create a method
A story about misunderstanding how to use java scanner (memo)
[Enum_help] Use enum_help to create a select box displayed in Japanese!
Let's create a TODO application in Java 13 TODO form validation 1: Character limit ・ Gradle update to use @Validated
How to create a new Gradle + Java + Jar project in Intellij 2016.03
Use Jenkins to build inside Docker and then create a Docker image.
How to study Java Silver SE 8
How to use Java HttpClient (Get)
Preparing to create a Rails application
How to make a Java container
How to use Java HttpClient (Post)
[Java] How to use join method
[Processing × Java] How to use variables
Create a Java project using Eclipse
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to make a Java array
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
To create a Zip file while grouping database search results in Java
I want to create a dark web SNS with Jakarta EE 8 with Java 11
[Java] How to use substring to cut out a part of a character string
[Java] Use jsoup to access HTTPS via a proxy that requires authentication
Hanashi stumbled a little on path trying to study Java with VScode
[Java] Create and apply a slide master
How to make a Java calendar Summary
A memorandum on how to use Eclipse
Multilingual Locale in Java How to use Locale
Whether to use Java Comparable or Comparator
Use TMDA to parse IBM Java javacore
[Java] How to use the HashMap class