Create a class, create a scanner object of the standard input stream in the main method of the class, prompt the user to enter an integer, and accept the integer through the scanner method, and then judge the remainder of the number and 2 through the ternary operator. , if the remainder is 0, it means that it is an even number, otherwise it is an odd number.
The code copy is as follows:
import java.util.Scanner;
public class ParityCheck {
public static void main(String[] args){
System.out.println("Please enter an integer:");
Scanner scanner=new Scanner(System.in);
long number = scanner.nextLong();
String check = (number % 2 == 0) ? "This number is: even" : "This number is: odd";
System.out.println(check);
}
}
The effect is shown in the figure: