Java basics

For my review

If there is "Enter 0 to 2", "Enter 0 to 2" is displayed. If there is "Enter character string", "Not an integer value" is displayed.

Because I could use the Scanner hasNext instead of using try-catch Record that there is such a way.

hello.java



      void getJudgeHand() {
        while (true) {
            System.out.println("command? 0:Goo 1:Par 2:Choki");
            if (scanner.hasNextInt()) {
                myHand = scanner.nextInt();
                if (myHand >= 0 && myHand <= 2) break;
            }
            else {
                scanner.next();
                System.out.println("Not an integer value");
            }
            System.out.println("Please enter from 0 to 2");
        }
    }

Try-catch method

hello.java




void getJudgeHand() {
    while (true) {
        try {
            System.out.println("command? 0:Goo 1:Par 2:Choki");

            this.myHand = scanner.nextInt();
            if (this.myHand <= 2 && this.myHand >= 0) { //Break if 2 or less and 0 or more
                break;
            }
        } catch (InputMismatchException e) {
            System.out.println("Not an integer value");
            scanner.next();    //Discard the input
        }
        System.out.println("Please enter from 0 to 2");
    }
}



Recommended Posts

Java basics
Java basics
Java basics
Java JAR basics
Object-oriented (Java) basics
Java concurrency basics
Java programming basics practice-array
Java
Java Network Basics (Communication)
Java
Muscle Java Basics Day 1
Basics of character operation (java)
Java programming basics practice-for statement
Summary of Java language basics
Java programming basics practice-switch statement
Getting Started with Java Basics
[Java11] Stream Usage Summary -Basics-
[Java basics] What is Class?
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
Rails basics
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Ruby basics
Java array
[Java] ArrayDeque
Ruby basics
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Fragment basics
java shellsort
[Java] compareTo
java reflexes
JPA Basics 1
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
Docker basics
ViewPager basics
[Java] Polymorphism
Java review
java framework